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

4 次查看(过去 30 天)
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.

采纳的回答

James Tursa
James Tursa 2013-9-24
编辑:James Tursa 2013-9-24
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.
  1 个评论
Abigail Osei-Asamoah
Thank you very much. I changed
int main ( ) { } to
void main ( ) { }
and moved it before the mexFunction and then i called main in the MexFunction as
void mexFunction( int nlhs, mxArray plhs[], int nrhs, const mxArray *prhs[] ) { main ( );} int main ( ) {main ( ) }
and it is working in MATLAB!
Thanks a bunch.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by