Yur*_*ras 6 c++ python boost boost-python archlinux
我有用C++编写的代码:
#include <boost/python.hpp>
char const* greet()
{
return "Yay!";
}
BOOST_PYTHON_MODULE(libtest)
{
using namespace boost::python;
def("greet", greet);
}
Run Code Online (Sandbox Code Playgroud)
现在我想通过以下方式将此动态库导入到python:
import libtest
Run Code Online (Sandbox Code Playgroud)
但我得到:
ImportError: /usr/lib/libboost_python.so.1.54.0: undefined symbol: PyClass_Type
Run Code Online (Sandbox Code Playgroud)
我该怎么办?我的操作系统是Arch Linux.
好的,我找到了解决这个问题的方法.最简单的选择是通过以下方式编译:
g++ testing.cpp -I/usr/include/python3.3m -I/usr/include/boost -lboost_python3 -lpython3.3m -o testing.so -shared -fPIC
Run Code Online (Sandbox Code Playgroud)
以前我使用-lboost_python而不是-lboost_python3 ......但是这个解决方案不是跨平台的,所以我们可以通过cmake实现这个目的:
cmake_minimum_required(VERSION 2.6)
find_package(Boost 1.54 EXACT REQUIRED COMPONENTS python3)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} "/usr/include/python3.3m/" )
find_package(PythonLibs)
ADD_LIBRARY(testing SHARED testing.cpp)
TARGET_LINK_LIBRARIES(testing ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
当然"/usr/include/python3.3m"不会是所有linux发行版中pythons包含目录的路径.