MATLAB: How to run mexFunction

running mexfunction

Hi there, this is my first post. I have a question in regards to running a C++ program from a Matlab GUI. I have spent hours looking through information online and I understand the method that is described is using MEX files. However, all of the tutorials and example I have found, including on the Mathworks site, are for relatively short C++ functions.
So, my question is: how do I get Matlab to run a large C++ program? It has about 20 cpp files which call upon each other to run the program. There is one Main function which then runs the entire program but I don't believe its enough just converting the main function, correct me if I'm wrong. Must I copy all of those files into Matlab and turn them all into MEX functions? That doesn't sound efficient to me but I am a novice in this field.

Best Answer

Just transform the one "main" function into a mexFunction. Then that will call all the underlying C++ functions. There is only one mexFunction ... your converted "main" function. The others are not mexFunctions per se, they are simply supporting code for your one mexFunction. To compile, be sure to include all source code .cpp files on the mex command line (do not put anything on the command line that is #include in your files such as header files). E.g., for 20 files you would have something like this:
> mex mymain.cpp source2.cpp source3.cpp source4.cpp ... source20.cpp
Where the ... are actually replaced by all the file names. I.e., list all the files on one line. Also, be sure your current default directory is the directory that has all your source code and header files. If you have any library files then include them on your mex command line along with the source code files.