简体   繁体   中英

Large C code not compiling with MATLAB's mex

My overall goal is to use a C model inside my MATLAB code. The C model is large (over a dozen .c files, which are all run from cModel.c) and can be successfully compiled then run in the terminal by

make cModel
cModel.x startingfile.inp

as the C model is correctly built for normal C compilers.

However, MATLAB's mex function is not compiling this C code. I'm a total novice with mex and I am pulling my hair out trying to understand what the problem is.

I think (and reading some similar problems on stackoverflow backs this up) that the problem is around creating a mexFunction. My attempt currently is

/*function AA_mexWrapper.c*/
/*Include the MATLAB mex header*/
#include "mex.h"

/* The gateway function */
void mexFunction( )
{
/* Main() of the C Model*/
cModel(); /* cModel writes files.  We don't care about the nonexistant returned variables*/
}

This generates the error (using mex AA_mexWrapper cModel) :

Error using mex
/Users/Filepath/ cModel.c:215:5: warning:
implicit declaration of function 'main' is invalid in C99 [-Wimplicit-    function-declaration]
main(int argc, char **argv);
^
/Users/Filepath/ cModel.c:215:10: error:
expected expression
main(int argc, char **argv);
     ^
1 warning and 1 error generated.

What is MATLAB doing and how do I fix it? I really just need it to treat cModel.c like a normal C compiler would.

PS. I have no idea what (int argc, char **argv) are in the C code. They are totally undefined, presumably they come from the optional user input of a filename containing non-default parameters for the model.

PPS. I will need to run the C model inside matlab by pointing it to a text file containing various model options. I hope that MATLAB can deal with this, but I'm starting to have my doubts...

You cannot call an executable like a function; the name of the executable is not "exported" the way you might think.

How about a simple solution? build your executable outside MATLAB and ask MATLAB to just run it; here's a piece of code that would do that (assuming that the cModel.x is in the same folder as the script/function that calls it):

system('./cModel.x startingfile.inp');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM