从matlab调用c函数我遇到了很多麻烦。
我的c功能很简单
test.c的
#include "mex.h"
int addOne(int a)
{
return a+1;
}
我在matlab命令窗口输入mex test.c,我收到此错误信息
Undefined symbols for architecture x86_64:
"_mexFunction", referenced from:
-exported_symbol[s_list] command line option
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mex: link of ' "test.mexmaci64"' failed.
My Matlab是2013a,osx 10.9,xcode 5.02
有人对此有所了解吗?感谢。
答案 0 :(得分:2)
这是一个让你入门的简单例子:
#include "mex.h"
double addOne(double a)
{
return a+1;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs!=1 || nlhs>1) mexErrMsgIdAndTxt("mex:error", "Wrong num of args");
if (!mxIsDouble(prhs[0])) mexErrMsgIdAndTxt("mex:error", "Not double");
plhs[0] = mxDuplicateArray(prhs[0]);
double *x = mxGetPr(plhs[0]);
size_t len = mxGetNumberOfElements(plhs[0]);
for (size_t i=0; i<len; ++i) {
x[i] = addOne(x[i]);
}
}
>> mex -largeArrayDims addOne.cpp
>> x = magic(4)
x =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> addOne(x)
ans =
17 3 4 14
6 12 11 9
10 8 7 13
5 15 16 2