python导入数学,嵌入Python,导入数学错误

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 "", line 1, in

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值