Embeded Python, import math error

故事扮演 提交于 2020-01-04 05:41:27

问题


First I'm on Mac OSX 10.12.6.

I want to embed a python environment in my C application. I take the github python project at https://github.com/python/cpython and manage to compile it with it's configure file and the command lines :

env LINKFORSHARED="-Wl,-stack_size,1000000  -framework CoreFoundation"
LDFLAGS="-Wl,-syslibroot,$SDK -arch i386" 
CFLAGS="-Os -isysroot $SDK -arch  i386"
./configure MACOSX_DEPLOYMENT_TARGET=10.6 --disable-shared --prefix=$PYTHON_PATH --exec-prefix=$PYTHON_PATH -build=i386-darwin
make
make altinstall 

($SDK point to MacOSX.sdk inside Xcode.app)

it gives me a libPython.a for 32bits then I redo it for 64bits and merge both with

lipo -create libPython32.A libPython64.a libPython32-64.a

In my XCode project, on .xconfig file, I import the lib and headers files with :

OTHER_LDFLAGS = $(inherited) -lpython32-64;
USER_HEADER_SEARCH_PATHS  = $(inherited) $(LIBS)/cpython35/include
LIBRARY_SEARCH_PATHS  = $(inherited) $(LIBS)/cpython35/lib;

Everything is found and my project compiles, no problem seen. And then in my python.c file (I reduced my path for this article, that's why '...') :

wchar_t *stdProgramName = L".../LIBs/cpython35/bin/python3.5";
Py_SetProgramName(stdProgramName);

wchar_t *stdPythonHome = L".../LIBs/cpython35";
Py_SetPythonHome(stdPythonHome);

wchar_t *stdlib = L".../LIBs/cpython35/lib/python3.5.zip:.../LIBs/cpython35/lib/python3.5:.../LIBs/cpython35/lib/python3.5/plat-darwin:.../LIBs/cpython35/lib/python3.5/lib-dynload:.../LIBs/cpython35/lib/python3.5/site-packages";
Py_SetPath(stdlib);

Py_Initialize();

// Run something
PyRun_SimpleString("import sys; print(sys.path)");
//To this line it's work fine, all path are correcte but then 
PyRun_SimpleString("import math;");

Py_Finalize();

the import of the math librarie does not work, it gives me :

Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: dlopen(.../LIBs/cpython35/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so, 2): Symbol not found: _PyExc_MemoryError
Referenced from: .../LIBs/cpython35/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so
Expected in: flat namespace
in .../LIBs/cpython35/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so

I'm kinda stuck at this point searching a solution for this import !!!


回答1:


In the end, I didn't need my python lib to be in a static library

So '--disable-shared' was the problem, I switched it with '--enable-shared'.

With this change, I get a libpython32.dylib, and that lib can access math.cpython-35m-darwin.so...

My process didn't change that much. In my project, I deleted -lpython32-64 in OTHER_LDFLAGS, and include libpython.dylib to "Link Binary With Libraries". And it works.



来源:https://stackoverflow.com/questions/48745053/embeded-python-import-math-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!