Search code examples
cmatlabmex

Matlab MEX C++ two files with same name in different folders


I'm trying to compile some C++ files that have the same name (fileA) but located in different folders (folder1 and folder2), lets say:

folder1/fileA.cpp
folder1/fileB.cpp

and

folder2/fileA.cpp
folder2/fileC.cpp

These files contain different classes.

But mex creates the objects for both folder1/fileA.cpp and folder2/fileA.cpp in the same folder so when it tries to generate the final W32 file some classes are missing.

Is there any way of telling mex to do not "flatten" the folders structure and then not overwriting one fileA.obj with the other fileA.obj?


All proposed solutions end up with the same basic idea: prepare my own compilation process (i.e. Makefiles or similar). My problem with this solution is that they are not the same for Linux, Windows, 32 or 64 bits,... and are not easy to maintain. That's why I would have preferred to have a solution based on mex but seems that it is something not supported so ...


Solution

  • If you need this kind of flexibility you will need to follow the conventional UNIX Makefile based strategy of seperately compiling each object file.

    For example, for each source file you can use mex -c with the -output directory option specified as in Oli's answer to create the object files - followed by a single final mex for the output function which use these object files.

    I can't think of any other way to acheive what you want (with a single call of the mex function).