MATLAB: How do i create a mex file from a C++ source codes that is an int main ( ) function

.cpp filescgateway routinemain functionmexmex-function

Dear All, I am trying to create a mex file from several C++ source codes with .cpp and .h extensions. So far i have figured that i have to create a gateway routine by adding a mex function to one of source C++ codes and #include "mex.h". I want to create the gateway routine in my main.cpp (which is a C++ file which calls other functions from other source code files). I have tried creating my gateway routine in the following manner void mexFunction( int nlhs, mxArray plhs[], int nrhs, const mxArray *prhs[] ) { main ( );} int main ( ) {/ C++ code calling other .cpp functions */ } I get an error when i try to compile this in Visual C++ 2010 (I am doing this because i want to check if code works in C++ before creating mex files) that says error C3861: 'main': identifier not found.
I have tried embedding the main.cpp function in the mex function but C++ doesn't allow a local function to be embedded in another. I do not know C++ but have learnt a little these past few days from google. The original source codes compile in C++ and output a text file with results and is a console application. How do i create a successful MEX file from such source code? Any help is appreciated.

Best Answer

Looks like you called main before it was declared. Try putting this prototype at the top of your code (prior to the mexFunction):
int main(void);
Another option would be to put your mexFunction code after the main() code.
Also, it would help us if you could format your question with the "{ } Code" button to make it more readable. Thanks.