How to compile and link several c++ files with *mex*

X

xz

I am programming some c++ codes to be called within Matlab.

If there is only one c++ file, life is simple. You simply mex the cpp
file (
with mexFunction(...) )and call the function within Matlab.

However, if you have several c++/h files, then what's standard
procedure to
compile and link the files?

for example, I have:

A.cpp (which contains the mexFunction and includes B.h)
B.h (contains the declarations of some data structures used in A.cpp)
B.cpp (implementation of the data structures declared in B.h)

The weird thing I found is that if you simply run:

mex B.cpp

It just compiles without complaining there is no mexFunction() or
main(),
which is not like the case if you gcc some c++ file without a main()
and
without using the option -c.

If you run

mex A.cpp

It also compiles without complaining that some function called in A
(defined
in B) has not be defined.


If you run

mex A.cpp B.cpp

you get an A.mexa64. However, when I call this A within Matlab, it
seems
like the functions defined in B and called in A are ignored without
being
called.

Anyway, I don't think I am compiling or linking in a correct way. "mex
--help" does not help very much.

Can you guys give some ideas about the compiling and linking of c++
with mex?
 
R

red floyd

xz said:
I am programming some c++ codes to be called within Matlab.

I[redacted]

Any specifics on compilation options, etc.. are OT.

Try comp.soft-sys.matlab
 
N

ngoonee

I am programming some c++ codes to be called within Matlab.

If there is only one c++ file, life is simple. You simply mex the cpp
file (
with mexFunction(...) )and call the function within Matlab.

However, if you have several c++/h files, then what's standard
procedure to
compile and link the files?

for example, I have:

A.cpp (which contains the mexFunction and includes B.h)
B.h (contains the declarations of some data structures used in A.cpp)
B.cpp (implementation of the data structures declared in B.h)

The weird thing I found is that if you simply run:

mex B.cpp

It just compiles without complaining there is no mexFunction() or
main(),
which is not like the case if you gcc some c++ file without a main()
and
without using the option -c.

If you run

mex A.cpp

It also compiles without complaining that some function called in A
(defined
in B) has not be defined.

If you run

mex A.cpp B.cpp

you get an A.mexa64. However, when I call this A within Matlab, it
seems
like the functions defined in B and called in A are ignored without
being
called.

Anyway, I don't think I am compiling or linking in a correct way. "mex
--help" does not help very much.

Can you guys give some ideas about the compiling and linking of c++
with mex?

This really is a Matlab question, not a c++ question. However, I do
recall that the Matlab documentation covers the issue of multiple
files being mex-ed. I've done a project before and the documentation
there was really everything I ever needed.

The alternative idea (and the method I'm using now) is to use a third-
party compiler. I'm using Visual Studio, and specific instructions are
available for it in the Matlab documentation (been doing it for a
couple of years). I'm pretty sure its possible with gcc and the like
as well, perhaps you should go looking around the documentation for
more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top