【问题标题】:Unable to link against Boost.Python on OS X无法在 OS X 上链接到 Boost.Python
【发布时间】:2015-11-11 14:45:35
【问题描述】:

我正在尝试使用 Boost.Python 构建一个非常简单的示例。我已经用自制软件安装了 boost 和 boost-python。我正在使用 python 3.4.3 和 boost 1.59。我的操作系统是 El Capitan。

Boost.Python 是这样与 python3 一起安装的:

brew install boost-python --with-python3

我有 3 个文件:

/* greet.hpp */
#ifndef BOOSTPYTHONHELLOWORLD_GREET_HPP
#define BOOSTPYTHONHELLOWORLD_GREET_HPP

char const* greet();

#endif //BOOSTPYTHONHELLOWORLD_GREET_HPP



/* greet.cpp */    
#include "greet.hpp"

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



/* greet_ext.cpp */
#include "greet.hpp"
#include <boost/python.hpp>

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

我的 CMakeLists.txt 文件如下所示:

cmake_minimum_required(VERSION 3.3)
project(BoostPythonHelloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PYTHON_LIBRARY "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/libpython3.4m.dylib")
set(PYTHON_INCLUDE_DIR "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/include/python3.4m")
FIND_PACKAGE(PythonLibs 3.4 REQUIRED)
FIND_PACKAGE(Boost COMPONENTS python)
if(NOT WIN32)
    add_definitions(-DBOOST_ALL_DYN_LINK=1)
    add_definitions(-DBOOST_TEST_DYN_LINK)
endif()
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
add_library(greet SHARED greet.cpp)
add_library(greet_ext SHARED greet_ext.cpp)
target_link_libraries(greet_ext greet)
target_link_libraries(greet_ext ${PYTHON_LIBRARIES})
target_link_libraries(greet_ext ${Boost_LIBRARIES})
set_target_properties(greet_ext PROPERTIES PREFIX "")

当我运行 CMake 时,它​​会找到所有正确的 python 库(因为我手动指定了它们,如您在上面的文件中所见)。

在构建过程中,我收到以下链接错误:

Scanning dependencies of target greet
[ 25%] Building CXX object CMakeFiles/greet.dir/greet.cpp.o
[ 50%] Linking CXX shared library libgreet.dylib
[ 50%] Built target greet
Scanning dependencies of target greet_ext
[ 75%] Building CXX object CMakeFiles/greet_ext.dir/greet_ext.cpp.o
[100%] Linking CXX shared library greet_ext.dylib
Undefined symbols for architecture x86_64:
  "boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:
      _PyInit_greet_ext in greet_ext.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [greet_ext.dylib] Error 1
make[1]: *** [CMakeFiles/greet_ext.dir/all] Error 2
make: *** [all] Error 2

有人知道为什么会这样吗?

编辑

如果我链接到 Python 2.7 它可以工作,这意味着 boost-python 是针对 python 2.7 而不是 3.4 构建的,即使我指定了 --with-python3 选项..

那么,问题是,如何针对 python 3.4 构建 boost-python?

【问题讨论】:

  • 你可以查看库名,应该是 boost_python_3 和 boost_python_2.7 之类的。但我不知道 FindBoost 是否会帮助你。您介意删除您的编辑并做出正确的回答吗?然后您可以接受您的答案,因为您的实际问题已经解决,并且可能对其他人有所帮助。
  • 可以添加cmake输出吗?我怀疑 CMake 选择了旧的 python 版本。 BOOST_PYTHON_MODULE 的含义对于 python 2 和 3 是不同的。似乎在编译过程中构建了 python 2 品种。

标签: cmake boost-python osx-elcapitan


【解决方案1】:

我目前使用的解决方案是在不支持 python 2.7 的情况下重新安装boost-python

我采取的步骤是:

  1. 如果已安装boost-python,请卸载:brew uninstall boost-python
  2. 安装boost-python,支持python3但不支持python 2.7:brew install boost-python --with-python3 --without-python

【讨论】:

  • 我执行了 brew install boost-python --with-python3 --without-python,cmake 找不到 boost (它与 brew install boost-python 一起使用) + cmake .. <.. .ominited> -- 找不到 Boost
  • 如果你之前安装过 boost-python,清除缓存文件夹/重启 MAC。缓存文件夹:\Library\Caches,否则cmake将无法定位到boost库。
【解决方案2】:

为了在 python 3 中使用 boost-python,您需要在 CMakeLists.txt 中进行更改:

FIND_PACKAGE(Boost COMPONENTS python)

到这里:

FIND_PACKAGE(Boost COMPONENTS python3)

这样你就可以重新编译带有 python 2 支持的 boost 并且仍然使用 python 3。

【讨论】:

    【解决方案3】:

    我建议通过brew 安装boost-python3

    您仍然需要提供次要版本号以及可能的库路径。以下对我有用:

    BOOST_LIBRARYDIR=/usr/local/Cellar/boost-python3/1.67.0/lib cmake ..

    用 CMakeLists.txt(位于..)包含

    FIND_PACKAGE(Boost COMPONENTS python36)

    (显然,对于 Boost 1.67.0 和 Python 3.6)。

    【讨论】:

      猜你喜欢
      • 2015-02-08
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多