c++ - 链接问题 Boost.Python

标签 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'

有什么想法吗?

最佳答案

此处在编译时为库提供正确、准确和有序的路径。请使用下面的命令并尝试,如果它不起作用 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/

相关文章:

c++ - 如何获取我的环境的当前语言环境?

c++ - 需要帮助从 .txt 文件输入变量

python-3.x - MacOS 上的 pip3 : SSL error while installing virtualenv

Python3 Tkinter 字体不工作

c++ - 修改 Boost Python 包装类?

c++ - 如何打包python模块依赖的共享对象?

c++ - 推断成员函数的返回类型

c++ - 标识符规则是否适用于运算符重载函数?

python - 基于固定相对路径在 zip 文件中包含目录结构

python - 提升 Python,将 C++ 回调传播到导致段错误的 Python