在macOS上进行boost.python编译时出现链接阶段错误

问题描述

我正在boost.python中为c ++代码编写一个python包装器,这是演示代码:

#include <boost/python.hpp>

char const* greet()
{
    return "hello,world";
}

BOOST_PYTHON_MODULE(test)
{
    using namespace boost::python;
    def("greet",&greet);
}

并且CMakeListFile编写为:

project(test)
cmake_minimum_required(VERSION 3.5)
set(BOOST_LIBRARIES /Users/pete/Documents/library/boost_1_74_0/stage/lib)
set(BOOST_INCLUDE_DIRS /Users/pete/Documents/library/boost_1_74_0)
# Find python and Boost - both are required dependencies
find_package(PythonLibs 3.7 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
message("Include dirs of boost: " ${BOOST_INCLUDE_DIRS} )
message("Libs of boost: " ${BOOST_LIBRARIES} )

message("Include dirs of Python: " ${PYTHON_INCLUDE_DIRS} )
message("Libs of Python: " ${PYTHON_LIBRARIES} )

# Without this,any build libraries automatically have names "lib{x}.so"
set(CMAKE_SHARED_MODULE_PREFIX "")

# Add a shared module - modules are intended to be imported at runtime.
# - This is where you add the source files
add_library(test MODULE test.cpp)

include_directories(
        test
        ${BOOST_INCLUDE_DIRS}
        ${PYTHON_INCLUDE_DIRS}
)

target_link_libraries(test
        ${BOOST_LIBRARIES}
        ${PYTHON_LIBRARIES}
        )

我确信打印的boost库路径是正确的,但是当进入链接阶段时,错误是:

[100%] Linking CXX shared module test.so
Undefined symbols for architecture x86_64:
  "_PyUnicode_Type",referenced from:
      boost::python::to_python_value<char const* const&>::get_pytype() const in test.cpp.o
  "__Py_NoneStruct",referenced from:
      boost::python::api::object::object() in test.cpp.o
  "boost::python::detail::init_module(PyModuleDef&,void (*)())",referenced from:
      _PyInit_text in test.cpp.o
  "boost::python::detail::gcc_demangle(char const*)",referenced from:
      boost::python::type_info::name() const in test.cpp.o
  "boost::python::detail::scope_setattr_doc(char const*,boost::python::api::object const&,char const*)",referenced from:
...
 

似乎链接路径不正确。如何解决?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)