链接问题Boost.Python

时间:2018-05-04 20:20:09

标签: c++ python-3.x boost-python

尝试使用Boost Python,但遇到链接问题。

代码,hello.cpp:

#include <boost/python.hpp>
char const* greet() {
  return "hellow, world";
}

int main() {
  return 0;
}

BOOST_PYTHON_MODULE(hello_ext) {
using namespace boost::python;
def("greet", greet);
}

编译:

g++ hello.cpp -L/usr/lib/x86_64-linux-gnu/ -I/usr/include/python3.5/ -lboost_python  /usr/lib/x86_64-linux-gnu/libpython3.5m.so -lboost_system

获取以下链接错误:

tmp/ccJ8ST4B.o: In function `PyInit_hello_ext':
hello.cpp:(.text+0x94): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyString_Size'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyUnicodeUCS4_FromEncodedObject'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyFile_FromString'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyString_Type'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyInt_Type'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyString_FromString'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyUnicodeUCS4_AsWideChar'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyString_FromStringAndSize'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `Py_InitModule4_64'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyString_FromFormat'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyNumber_Divide'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyNumber_InPlaceDivide'
/usr/lib/x86_64-linux-gnu//libboost_python.so: undefined reference to `PyInt_AsLong'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这里编译时为库提供了正确,精确和有序的路径。请使用以下命令并尝试,如果它不工作set LD_LIBRARY_PATH(仅在程序目录路径中设置)到程序目录路径。这会将所有必需的库链接到您的程序目录。

g++  -I/usr/include/python3.5/  /usr/lib/x86_64-linux-gnu/libpython3.5m.so  -L/usr/lib/x86_64-linux-gnu/ -lboost_python -lboost_system hello.cpp -o hello

如果以上行不起作用

export LD_LIBRARY_PATH=/program_path:$LD_LIBRARY_PATH