Matlab编译mex文件时找不到标准的C库

4

更新:我安装了XCode并在mexopts.sh中更改了SDKROOT以反映实际路径,如此处所述,但是当我包含mex.h时仍然出现此错误:

In file included from /Applications/MATLAB_R2012b.app/extern/include/matrix.h:294,
                 from /Applications/MATLAB_R2012b.app/extern/include/mex.h:58,
                 from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/tmwtypes.h:61:21: error: float.h: No such file or directory

    mex: compile of ' "test.c"' failed.

我正在尝试通过mex来编译一个简单的C文件,以便在Matlab中使用,但似乎从未找到我知道已安装的默认库。例如,尝试编译以下内容:

#include <string.h>
int main() {
    return 0;
}

出现错误:

test.c:1:20: error: string.h: No such file or directory
mex: compile of ' "test.c"' failed.

尽管使用gcc编译没有问题,但更糟糕的是,当我尝试这样包含mex.h时:

#include "mex.h"
int main() {
    return 0;
}

I get the following error:

In file included from /Applications/MATLAB_R2012b.app/extern/include/mex.h:58,
                 from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:293:20: error: stddef.h: No such file or directory
In file included from /Applications/MATLAB_R2012b.app/extern/include/matrix.h:294,
                 from /Applications/MATLAB_R2012b.app/extern/include/mex.h:58,
                 from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/tmwtypes.h:43:20: error: limits.h: No such file or directory
/Applications/MATLAB_R2012b.app/extern/include/tmwtypes.h:46:21: error: stdbool.h: No such file or directory
/Applications/MATLAB_R2012b.app/extern/include/tmwtypes.h:61:21: error: float.h: No such file or directory
/Applications/MATLAB_R2012b.app/extern/include/tmwtypes.h:777:2: error: #error "This code must be compiled using a 2's complement representation for signed integer values"
In file included from /Applications/MATLAB_R2012b.app/extern/include/matrix.h:294,
                 from /Applications/MATLAB_R2012b.app/extern/include/mex.h:58,
                 from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/tmwtypes.h:823: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CHAR16_T'
In file included from /Applications/MATLAB_R2012b.app/extern/include/mex.h:58,
                 from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:319: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mxChar'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:375: error: expected ')' before 'n'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:383: error: expected ')' before 'n'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:397: error: expected declaration specifiers or '...' before 'size_t'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:590: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mxGetNumberOfElements'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:632: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:688: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mxGetM'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:700: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mxGetN'
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:750: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'mxGetElementSize'
In file included from /Applications/MATLAB_R2012b.app/extern/include/mex.h:58,
             from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:851:20: error: stdlib.h: No such file or directory
/Applications/MATLAB_R2012b.app/extern/include/matrix.h:1072: error: expected ')' before 'm'
In file included from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/mex.h:91: error: expected specifier-qualifier-list before 'size_t'
In file included from test.c:2:
/Applications/MATLAB_R2012b.app/extern/include/mex.h:161:19: error: stdio.h: No such file or directory

    mex: compile of ' "test.c"' failed.

我猜测mex没有在正确的位置寻找这些库文件,或者安装文件已经损坏了,但我不知道如何修复它。
Mac OSX Mountain Lion + XCode 4.5.2, Matlab R2012b, gcc 4.2.1

如果你甚至没有包含任何库,那你能编译吗? - Dennis Jaheruddin
如果我不包含任何库并删除对mex特定类型(如mxArray)的任何引用,则编译正常。 - David Pfau
3个回答

2

包含文件需要在您的PATH中(输入path以显示这些文件夹),除非您使用mex -Ipathname特别包含它们。您可以单击“文件->设置路径”并添加包含string.h的文件夹。


1

1
使用mex命令指定以下选项即可正常工作。请确保您在以下文件夹中拥有标准的头文件,否则请相应更新:
mex -I'C:\Program Files (x86)\Windows Kits\10\Include\10.0.10150.0\ucrt'.

如果您成功解决了这个错误,可能还需要添加 .lib 文件。类似地加入即可。
mex -I'C:\Program Files (x86)\Windows Kits\10\Include\10.0.10150.0\ucrt' -L'C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10150.0\ucrt\x64'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接