PythonをOSXのMATLABmex関数に埋め込もうとしています。これを実行できるという参照を確認しましたが(ここなど)、OSX固有の情報が見つかりません。これまでのところ、組み込みPythonを正常にビルドでき(リンカーフラグはOKである必要があります)、問題なくデフォルトオプションを使用してサンプル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
しかし、Pythonを埋め込んだmexファイルを作成しようとすると、未定義のシンボルmainで問題が発生します。これが私の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();
}
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
arch設定(すべてを32ビットに保つために-arch i386を追加しました-python.org32ビット2.5ビルドを使用しています)とリンカーフラグの順序を試してみましたが、できませんでしたどこにでも行くことができます。オンラインでもあまり見つかりません。誰かがこれを構築する方法について何かアイデアがありますか?
[編集:おそらく、MATLAB 7.8(r2009a)、Python 2.5.4(python.org)を搭載したOS X 10.6.1を追加する必要があります-gcc-4.0とgcc-4.2(apple)の両方を試しました]