【发布时间】:2014-10-01 01:39:59
【问题描述】:
我正在尝试使用 boost::python 库在 C++ 中为 Python3 创建一个 helloWorld 模块。
这是CmakeList.txt:
set(Python_ADDITIONAL_VERSIONS 3.4)
find_package( PythonLibs 3.4 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )
find_package( Boost 1.56.0 EXACT COMPONENTS python3 REQUIRED )
include_directories( ${Boost_INCLUDE_DIR} )
# Define the wrapper library that wraps our library
add_library( hello SHARED main.cpp )
target_link_libraries( hello ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES} )
# don't prepend wrapper library name with lib
set_target_properties( hello PROPERTIES PREFIX "" OUTPUT_NAME hello)
main.cpp
#include <boost/python.hpp>
char const* greet( )
{
return "Hello world";
}
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
def( "greet", greet );
}
我从here 中描述的源代码安装了 boost 库,但它不允许我使用 boost-python3 库(Cmake 中有错误)。为此我使用了
./bootstrap.sh --with-python-version=3.4 --prefix=/usr/local
而不是
./bootstrap.sh --prefix=/usr/local
明确指定python的版本;
作为输出,我们得到一个共享库hello.so。一切似乎都很好。但是……
当我尝试将库导入 python 脚本sript.py 时:
import hello
在终端中使用命令 ...$ python3 script.py
我收到一个错误
Traceback (most recent call last):
File "script.py", line 1, in <module>
import hello
ImportError: /usr/local/lib/libboost_python3.so.1.56.0: undefined symbol: PyClass_Type
问题是:如何让boost库兼容python3? python2 没有问题。但我需要python3。
当同样的错误发生时,我也看到了page,但这对我没有帮助。
我的软件:
- 提升版本 1.56.0
- pyhton 3.4
- cmake 版本 2.8.12.2
- gcc 4.8.2
- 操作系统:Ubuntu 14.04 LTS,64 位
【问题讨论】:
-
不应该
${PythonLibs_LIBRARIES}是${PYTHON_LIBRARIES}? -
我将
${PythonLibs_LIBRARIES}替换为${PYTHON_LIBRARIES}后错误是一样的 -
您尝试过这里提到的解决方案吗? stackoverflow.com/questions/19865757/…
-
是的,我做到了。 (见最后两句,并附在page这个词上)
标签: c++ python-3.x boost cmake boost-python