gpt4 book ai didi

c++ - 链接问题 Boost.Python

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:08 25 4
gpt4 key购买 nike

尝试使用 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

关于c++ - 链接问题 Boost.Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50182539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com