编译时与 Boost.python 链接错误

Posted

技术标签:

【中文标题】编译时与 Boost.python 链接错误【英文标题】:Linking error with Boost.python while compiling 【发布时间】:2020-02-17 06:45:55 【问题描述】:

我正在尝试构建受here启发的示例

我的初衷是能够在 Python 中调用 C++ 代码。为了实现这个目标,我只是运行 Boost 文档中的示例。

这是我的生成文件

CXX               := g++
CXX_FLAGS         := -Wall -Wextra -std=c++17 -ggdb

BIN               := bin
SRC               := src
INCLUDE           := inc
LIB               := lib

BOOST_ROOT        := /home/eicossa/repo/boost_install
BOOST_INCL        := $BOOST_ROOT/include/
BOOST_LIBS        := $BOOST_ROOT/libs/ 

PYTHON_INCL       := /usr/include/python3.7m/
PYTHON_LIBS       := /usr/lib/python3.7/

LIBFLAGS          := -lboost_system 
EXECUTABLE        := greet


all: $(BIN)/$(EXECUTABLE)

run: clean all
        clear
        ./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp 
        $(CXX) $(CXX_FLAGS) -I$(PYTHON_INCL) -I$(INCLUDE) -I$(BOOST_INCL) -L$(BOOST_LIBS) -L$(PYTHON_LIBS) -L$(LIB$

clean:
        -rm $(BIN)/*

这会导致以下错误

/tmp/ccNVjFX3.o:在函数中boost::python::api::object::object()': /home/eicossa/repo/boost_install/include/boost/python/object_core.hpp:400: undefined reference to_Py_NoneStruct' /tmp/ccQNpbxG.o:在函数中 PyInit_hello_ext': /home/eicossa/Dropbox/wtcd/license/boost/src/bindings.cpp:3: undefined reference toboost::python::detail::init_module(PyModuleDef&, void ()())' /tmp/ccQNpbxG.o: 在函数boost::python::type_info::name() const': /home/eicossa/repo/boost_install/include/boost/python/type_id.hpp:160: undefined reference toboost::python::detail::gcc_demangle(char const)' /tmp/ccQNpbxG.o: 在函数中 boost::python::to_python_value<char const* const&>::operator()(char const* const&) const': /home/eicossa/repo/boost_install/include/boost/python/converter/builtin_converters.hpp:157: undefined reference to boost::python::converter::do_return_to_python(char const*)' /tmp/ccQNpbxG.o:在函数中boost::python::to_python_value<char const* const&>::get_pytype() const': /home/eicossa/repo/boost_install/include/boost/python/converter/builtin_converters.hpp:157: undefined reference toPyUnicode_Type' /tmp/ccQNpbxG.o:在函数中 void boost::python::def<char const* (*)()>(char const*, char const* (*)())': /home/eicossa/repo/boost_install/include/boost/python/def.hpp:91: undefined reference toboost::python::detail::scope_setattr_doc(char const*, boost::python::api::object const&, char const*)' /tmp/ccQNpbxG.o:在函数`boost::python::api::object boost::python::detail::make_function_aux

(char const* (*)(), boost::python::default_call_policies const&, boost::mpl::vector1 const&)': /home/eicossa/repo/boost_install/include/boost/python/make_function.hpp:38: 未定义的引用 boost::python::objects::function_object(boost::python::objects::py_function const&)' /tmp/ccQNpbxG.o: In function boost::python::objects::py_function_impl_base::py_function_impl_base()': /home/eicossa/repo/boost_install/include/boost/python/object/py_function.hpp:20: 对vtable for boost::python::objects::py_function_impl_base' /tmp/ccQNpbxG.o:(.rodata._ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE[_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE]+0x30): undefined reference to boost::python::objects::py_function_impl_base::max_arity() const' 的未定义引用 /tmp/ccQNpbxG.o:在函数中 boost::python::objects::caller_py_function_impl<boost::python::detail::caller<char const* (*)(), boost::python::default_call_policies, boost::mpl::vector1<char const*> > >::~caller_py_function_impl()': /home/eicossa/repo/boost_install/include/boost/python/object/py_function.hpp:30: undefined reference to boost::python::objects::py_function_impl_base::~py_function_impl_base()' /tmp/ccQNpbxG.o:(.rodata._ZTIN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE[_ZTIN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS 对typeinfo for boost::python::objects::py_function_impl_base' /tmp/ccQNpbxG.o: In functionboost::python::converter::expected_pytype_for_arg::get_pytype()' 的未定义引用: /home/eicossa/repo/boost_install/include/boost/python/converter/pytype_function.hpp:69: 未定义的引用 boost::python::converter::registry::query(boost::python::type_info)' /home/eicossa/repo/boost_install/include/boost/python/converter/pytype_function.hpp:70: undefined reference to boost::python::converter::registration::expected_from_python_type() const' collect2:错误:ld 返回 1 退出状态 Makefile:27:配方 对于目标 'bin/greet' 失败的 make: *** [bin/greet] 错误 1

我做错了什么?

【问题讨论】:

What is an undefined reference/unresolved external symbol error and how do I fix it?的可能重复 【参考方案1】:
CXX               := g++
CXX_FLAGS         := -Wall -Wextra -std=c++17 -ggdb

BIN               := bin
SRC               := src
INCLUDE           := inc
LIB               := lib

BOOST_ROOT        := /home/eicossa/repo/boost_install
BOOST_INCL        := $BOOST_ROOT/include/
BOOST_LIBS        := $BOOST_ROOT/libs/ 

PYTHON_INCL       := /usr/include/python3.7m/
PYTHON_LIBS       := /usr/lib/python3.7/

LIBFLAGS          := -lboost_system  -lboost_python37 -lpython3.7m #changes made here
EXECUTABLE        := greet


all: $(BIN)/$(EXECUTABLE)

run: clean all
        clear
        ./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp 
        $(CXX) $(CXX_FLAGS) -I$(PYTHON_INCL) -I$(INCLUDE) -I$(BOOST_INCL) -L$(BOOST_LIBS) -L$(PYTHON_LIBS) -L$(LIB$

clean:
        -rm $(BIN)/*

在 Makefile 中尝试这些更改

【讨论】:

以上是关于编译时与 Boost.python 链接错误的主要内容,如果未能解决你的问题,请参考以下文章

链接 Boost.python 示例的问题

Boost Python,Visual Studio链接到错误的boost dll

Boost.Python 和 CMake 链接和加载错误

Boost.Python.ArgumentError:python str 未转换为 std::string

使用 Boost::Python::Object 会导致链接器错误

为 ARM 交叉编译应用程序时与 sdl 链接时出错