繁体   English   中英

链接问题Boost.Python

[英]Linking issues 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'

有任何想法吗?

在编译时,这里给出了正确,准确和有序的库路径。 请使用以下命令尝试,如果不起作用, 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM