【问题标题】:Error building Python extension using boost.python使用 boost.python 构建 Python 扩展时出错
【发布时间】:2016-08-10 07:01:30
【问题描述】:

我在装有 OS X 10.11.6 的 Mac 上使用 homebrew 安装了 boost 以及 boost-pythonboost-build。我正在运行 Python 3.5.2。 boost 已正确设置并适用于 C++ 项目。 user-config.jam 和我的 python 扩展项目目录中的 jamfile 都可以。我试图从以下来源编译一个共享库

#include <iostream>
#include <boost/python.hpp>

using namespace std;


void say_hello() {
    std::cout << "HELLO!!!!!";
}

BOOST_PYTHON_MODULE(hello) {
    using namespace boost::python;
    def ("say_hello", say_hello);
}

使用b2 解释器。它发出以下命令:

"g++" -dynamiclib -Wl,-single_module -install_name "hello.so" -L"/usr/local/lib/python3.5" -o "bin/darwin-4.2.1/release/hello.so" "bin/darwin-4.2.1/release/say_hello.o"  -lpython3.5    -headerpad_max_install_names -Wl,-dead_strip -no_dead_strip_inits_and_terms

,与

一起崩溃

darwin.link.dll bin/darwin-4.2.1/release/hello.so

架构 x86_64 的未定义符号:

“boost::python::objects::py_function_impl_base 的类型信息”,引用自:

[...长追溯...]

“boost::python::detail::init_module(PyModuleDef&, void (*)())”,引用自:

say_hello.old 中的_PyInit_hello:未找到架构 x86_64 的符号

我非常了解有关类似问题的所有问题,但不幸的是,它们都没有提供有效的答案。

我需要做什么才能让这个简单的代码作为 Python 扩展模块工作?

【问题讨论】:

    标签: c++ python-3.5 boost-python


    【解决方案1】:

    您也应该将它与 boost python 库链接(因为 boost.python 不仅仅是标题)。以下是构建命令中包含 boost 库的方式(我机器上的路径):

    -L/usr/lib/libpython2.7.dylib /usr/local/lib/libboost_system-mt.dylib /usr/local/lib/libboost_python-mt.dylib /usr/lib/libpython2.7.dylib -Wl,-rpath,/usr/lib/libpython2.7.dylib
    

    我假设你可以不用 libboost_system 库。 (我在运行make VERBOSE=1 时得到这样的输出,因为我没有明确地运行make。)

    关于cmake,这里有一个简单的CMakeLists.txt,你可以用Boost.Python来构建一个项目:

    cmake_minimum_required(VERSION 2.8)
    
    set(LIBRARY_NAME "ext") # ext for extension
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -ggdb")
    
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
    
    set(SOURCES
        python_interface.cpp
        main.cpp
    )
    
    set(CMAKE_MACOSX_RPATH ON)
    
    # Get Boost
    find_package(Boost COMPONENTS
        system
        python REQUIRED)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    
    # Get Python
    find_package(PythonLibs REQUIRED)
    include_directories(${PYTHON_INCLUDE_DIRS})
    link_directories(${PYTHON_LIBRARIES})
    
    add_library(${LIBRARY_NAME} SHARED ${SOURCES})
    
    target_link_libraries(${LIBRARY_NAME} 
        ${Boost_LIBRARIES}
        ${PYTHON_LIBRARIES}
    )
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      相关资源
      最近更新 更多