4

I have a simulink model with a c++ s-function. This s-function needs access to a lot of (>50) classes. Each class is consists of a header (.h) and a source (.cpp) file. I have also divided my code into multiple directories:

root
-sfun.cpp
-folder1
--file1.h
--file1.cpp
--file2.h
--file2.cpp
-folder2
--file3.h
--file3.cpp
...

For the compilation of the s-function I am using the mex-function:

mex -Ifolder1 -Ifolder2 -outdir bin sfun.cpp folder1/file1.cpp folder1/file2.cpp folder1/file3.cpp

(http://de.mathworks.com/help/matlab/ref/mex.html)

But this gets very long and ugly with more and more files because I need to specify each header folder and earch source file separately. Is there a better way to create a mex file that needs access to lots of source files?

I have the following ideas, but I am not sure what could be the correct and easiest way:

  1. Add all header and source files (fileX.h/ fileX.cpp) to an visual studio project and compile them to a *.lib file. Then compile only the sfun.cpp with the mex tool and provide access to the *.lib file

  2. Move all header and source files into one directory. This would shorten the command line as follows:

    mex -outdir bin sfun.cpp file1.cpp file2.cpp file3.cpp
    
  3. Making everything inline so that there is no need for a source file. (very ugly solution)

  4. Is there some kind of a makefile for the mex compiler?

  5. Include not only the header files but also the source files via a #include directive.

At the moment I am not convinced of any of these ideas and I would appreciate any help.

Thanks

Edit1: One annotation: This project should be ported to a dspace pc in a later stage. Do I need to consider something special in this case?

Gustav-Gans
  • 365
  • 1
  • 3
  • 13
  • 2
    Pretty much option 1. Building a separate library is what I usually suggest. Or to build the MEX file itself [in Visual Studio](http://stackoverflow.com/a/27391300/2778484). – chappjc Feb 04 '15 at 16:10
  • How did this work out? What did you end up doing? I'm trying to do something similar. – audrow May 13 '17 at 00:18
  • I ended up with a script that creates me that command line every time – Gustav-Gans Aug 13 '17 at 18:46

0 Answers0