gpt4 book ai didi

c++ - 使用 boost.python 构建 Python 扩展时出错

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:54 25 4
gpt4 key购买 nike

我在我的 Mac 上使用 homebrew 安装了 boost 以及 boost-pythonboost-build使用 OS X 10.11.6。我正在运行 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

Undefined symbols for architecture x86_64:

"typeinfo for boost::python::objects::py_function_impl_base", referenced from:

[...long trace back ...]

"boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:

_PyInit_hello in say_hello.o ld: symbol(s) not found for architecture x86_64

我非常清楚所有关于类似问题的问题,但不幸的是,没有一个提供有效的答案。

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

最佳答案

您还应该将其与 boost python 库链接(因为 boost.python 不仅仅是 header )。以下是如何将 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}
)

关于c++ - 使用 boost.python 构建 Python 扩展时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38854937/

25 4 0
文章推荐: html - 用曲线制作div?
文章推荐: html - 标签对
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com