【问题标题】:call c function from matlab从matlab调用c函数
【发布时间】:2014-09-26 03:11:54
【问题描述】:

我在 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.

我的 Matlab 是 2013a,osx 10.9,xcode 5.02

有人对此有任何想法吗?谢谢。

【问题讨论】:

  • 我认为您可能需要阅读更多关于mex 的信息。你的网关功能在哪里,mexFunction?尝试启动here,或任何其他示例。 More here.

标签: c matlab mex


【解决方案1】:

下面是一个快速入门的示例:

addOne.cpp

#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]);
    }
}

MATLAB:

>> 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2015-04-08
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多