大型 C 代码无法使用 MATLAB 的 mex 进行编译

Large C code not compiling with MATLAB's mex

我的总体目标是在我的 MATLAB 代码中使用 C 模型。 C模型很大(十几个.c文件,都是cModel.c的运行),然后在终端运行

编译成功
make cModel
cModel.x startingfile.inp

因为 C 模型是为普通 C 编译器正确构建的。

但是,MATLAB 的 mex 函数不编译此 C 代码。我是 mex 的新手,我正在努力了解问题所在。

我认为(并且在 Whosebug 上阅读了一些类似的问题支持了这一点)问题在于创建 mexFunction。我目前的尝试是

/*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*/
}

这会产生错误(使用 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.

MATLAB 在做什么,我该如何修复它?我真的只需要它像普通 C 编译器那样对待 cModel.c。

PS。我不知道 C 代码中的 (int argc, char **argv) 是什么。它们完全未定义,大概它们来自包含模型非默认参数的文件名的可选用户输入。

PPS。我需要 运行 matlab 中的 C 模型,方法是将它指向包含各种模型选项的文本文件。我希望 MATLAB 可以处理这个问题,但我开始怀疑...

您不能像调用函数那样调用可执行文件;可执行文件的名称与您可能想的 "exported" 不同。

简单的解决方案怎么样?在 MATLAB 之外构建您的可执行文件并要求 MATLAB 仅 运行 它;这里有一段代码可以做到这一点(假设 cModel.x 与调用它的 script/function 在同一个文件夹中):

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