简体   繁体   English

在os x上的matlab mex文件中嵌入python

[英]embed python in matlab mex file on os x

I'm trying to embed Python into a MATLAB mex function on OS X. I've seen references that this can be done (eg here ) but I can't find any OS X specific information. 我正在尝试将Python嵌入到OS X上的MATLAB mex函数中。我已经看到了可以这样做的引用(例如这里 )但我找不到任何OS X特定信息。 So far I can successfully build an embedded Python (so my linker flags must be OK) and I can also build example mex files without any trouble and with the default options: 到目前为止,我可以成功构建一个嵌入式Python(所以我的链接器标志必须正常),我也可以毫无困难地使用默认选项构建示例mex文件:

jm-g26b101:mex robince$ cat pytestnomex.c
#include <Python/Python.h>

int main() {
  Py_Initialize();
  PyRun_SimpleString("print 'hello'"); 
  Py_Finalize();
  return 0;
}
jm-g26b101:mex robince$ gcc -arch i386 pytestnomex.c -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -L/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/config -ldl -lpython2.5
jm-g26b101:mex robince$ ./a.out
hello

But when I try to build a mex file that embeds Python I run into a problem with undefined symbol main. 但是当我尝试构建一个嵌入Python的mex文件时,我遇到了一个未定义符号main的问题。 Here is my mex function: 这是我的mex功能:

#include <Python.h>
#include <mex.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])
{
    mexPrintf("hello1\n");
    Py_Initialize();
    PyRun_SimpleString("print 'hello from python'");
    Py_Finalize();
}

Here are the mex compilation steps: 以下是mex编译步骤:

jm-g26b101:mex robince$ gcc -c  -I/Applications/MATLAB_R2009a.app/extern/include -I/Applications/MATLAB_R2009a.app/simulink/include -DMATLAB_MEX_FILE  -arch i386 -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "pytest.c"
jm-g26b101:mex robince$ gcc -O  -arch i386 -L/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/config -ldl -lpython2.5 -o  "pytest.mexmaci"  pytest.o  -L/Applications/MATLAB_R2009a.app/bin/maci -lmx -lmex -lmat -lstdc++
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I've tried playing around with arch settings (I added -arch i386 it to try and keep everything 32bit - I am using the python.org 32 bit 2.5 build), and the order of the linker flags, but haven't been able to get anywhere. 我已经尝试过使用arch设置(我添加了-arch i386它尝试保留所有32位 - 我使用python.org 32位2.5版本),以及链接器标志的顺序,但是还没有能够到处都是 Can't find much online either. 在网上也找不到多少。 Does anyone have any ideas of how I can get this to build? 有没有人对如何构建这个有任何想法?

[EDIT: should probably add I'm on OS X 10.6.1 with MATLAB 7.8 (r2009a), Python 2.5.4 (python.org) - I've tried both gcc-4.0 and gcc-4.2 (apple)] [编辑:应该添加我在OS X 10.6.1上使用MATLAB 7.8(r2009a),Python 2.5.4(python.org) - 我已经尝试过gcc-4.0和gcc-4.2(苹果)]

I think I found the answer - by including the mysterious apple linker flags: 我想我找到了答案 - 包括神秘的苹果链接器标志:

-undefined dynamic_lookup -bundle

I was able to get it built and it seems to work OK. 我能够把它建成它似乎工作正常。 I'd be very interested if anyone has any references about these flags or library handling on OS X in general. 如果有人在OS X上有任何关于这些标志或库处理的引用,我会非常感兴趣。 Now I see them I remember being bitten by the same thing in the past - yet I'm unable to find any documentation on what they actually do and why/when they should be needed. 现在我看到它们我记得曾经被同样的事情所困扰 - 但我无法找到任何关于它们实际做什么以及为什么/何时应该需要的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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