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

3 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

James Tursa
James Tursa le 24 Sep 2013
Modifié(e) : James Tursa le 24 Sep 2013
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 commentaire
Abigail Osei-Asamoah
Abigail Osei-Asamoah le 24 Sep 2013
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by