无法链接到Boost.Python OS X [英] Unable to link against Boost.Python on OS X

查看:212
本文介绍了无法链接到Boost.Python OS X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a really trivial example with Boost.Python. I have installed boost and boost-python with homebrew. I am using python 3.4.3 and boost 1.59. My OS is El Capitan.

Boost.Python与python3一起安装,如下所示:

Boost.Python was installed with python3 like this:

brew install boost-python --with-python3

我有3个文件: / p>

I have 3 files:

/* 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文件如下所示:

My CMakeLists.txt file looks like this:

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库(因为我手动指定它们,你可以在上面的文件中看到)。

When I run CMake it finds all the correct python libraries (because I specified them manually as you can see in the file above).

在生成期间,我得到以下链接错误: / p>

During build I get the following link error:

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

想法为什么这是发生?

Anyone have any idea why this is happening?

EDIT

如果我链接到Python 2.7,即使我指定了 - with-python3 选项,python也是基于python 2.7而不是3.4构建的。

If I link against Python 2.7 it works, which means boost-python was built against python 2.7 instead of 3.4, even though I specified the --with-python3 options..

所以,问题是,如何构建boost-python对python 3.4?

So, the questions is, how to build boost-python against python 3.4?

推荐答案

我目前使用的解决方案是重新安装 boost-python 没有python 2.7支持。

The solution I am currently using is to reinstall boost-python without python 2.7 support.

我采取的步骤是:


  1. 卸载 boost-python 如果已经安装: brew uninstall boost-python

  2. 使用python3支持但不支持python 2.7安装 boost-python brew install boost-python - with-python3 --without-python

  1. Uninstall boost-python if already installed: brew uninstall boost-python.
  2. Install boost-python with python3 support but without python 2.7 support: brew install boost-python --with-python3 --without-python.

这篇关于无法链接到Boost.Python OS X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆