1.Python/dynload_shlib.c:94: undefined reference to `dlsym'

to solve the problems such as under Ubuntu:

---------------------------------------------------------------------------------------

Python/dynload_shlib.c:94: undefined reference to `dlsym'
dynload_shlib.c:130: undefined reference to `dlopen'
Python/dynload_shlib.c:141: undefined reference to `dlsym'
Python/dynload_shlib.c:133: undefined reference to `dlerror'

----------------------------------------------------------------------------------------

To solve this linking problem, run 'python-config --libs', then you got the dynamic library list. Find where these libs are, and add their paths to LD_LIBRARY_PATH before 'scons ./build/ARM/gem5.opt'

Finally, it works!

 

2.PyImport_Import() fail:

ImportError: /usr/local/lib/python2.7/lib-dynload/_socket.so: undefined symbol: PyExc_ValueError

如果python在编译安装的时候,没有使用:
./configure --enable-shared
那么就会造成在/usr/local/lib/目录下只有libpython2.7.a而没有libpython2.7.so,这个时候需要给makefile加一个参数:
C_FLAGS += -export-dynamic

或者重新使用./configure --enable-shared编译Python.

 

3.C/C++多线程调用Python

Python中有一个GIL,导致Python线程效率很低,此外若在C/C++中创建线程来调用Python解释器,会造成段错误。

解决方式:

//主线程中

Py_Initialize();

PyEval_InitThreads();
PyEval_ReleaseLock();   //启动子线程前执行,为了释放PyEval_InitThreads获得的全局锁,否则子线程可能无法获取到全局锁。

 

//子线程中

PyGILState_STATE state = PyGILState_Ensure();

.....    //调用Python代码

PyGILState_Release(state);

 

//主线程中

//保证子线程调用都结束后
PyGILState_Ensure();
Py_Finalize();
//之后不能再调用任何Python的API