簡體   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