Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cmake 无法在OS X上链接Boost.Python_Cmake_Boost Python_Osx Elcapitan - Fatal编程技术网

Cmake 无法在OS X上链接Boost.Python

Cmake 无法在OS X上链接Boost.Python,cmake,boost-python,osx-elcapitan,Cmake,Boost Python,Osx Elcapitan,我试图用Boost.Python构建一个非常简单的示例。我已经用自制软件安装了boost和boostpython。我使用的是python 3.4.3和boost 1.59。我的操作系统是El Capitan Boost.Python与python3一起安装,如下所示: brew install boost-python --with-python3 cmake_minimum_required(VERSION 3.3) project(BoostPythonHelloWorld) set(CM

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

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

brew install boost-python --with-python3
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 "")
我有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);
}
当我运行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
有人知道为什么会这样吗

编辑

如果我链接到Python2.7,它会工作,这意味着boost Python是根据Python2.7而不是3.4构建的,尽管我指定了
--with-python3
选项


因此,问题是,如何针对python 3.4构建boost python

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

我采取的步骤是:

  • 卸载
    boostpython
    如果已经安装:
    brew卸载boostpython
  • 安装
    boostpython
    时支持python3,但不支持python2.7:
    brew安装boostpython——使用-python3——不支持python

  • 为了在python 3中使用boost python,您需要在CMakeLists.txt中更改以下内容:

    FIND_PACKAGE(Boost COMPONENTS python)
    
    为此:

    FIND_PACKAGE(Boost COMPONENTS python3)
    

    这样,您可以使用python 2支持重新编译boost,并且仍然使用python 3。

    我建议通过
    brew
    安装
    boost-python3

    您还需要提供次要版本号,可能还需要提供库路径。以下几点对我很有用:

    BOOST\u LIBRARYDIR=/usr/local/cillar/BOOST-python3/1.67.0/lib-cmake..

    CMakeLists.txt(位于
    )包含

    FIND_包(Boost COMPONENTS python36)


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

    您可以检查库名,应该是Boost_Python_3和Boost_Python_2.7或类似的名称。但我不知道FindBoost是否能帮到你。你介意删除你的编辑并做出正确的回答吗?然后,您可以接受您的答案,因为您的实际问题已经解决,并且可能会帮助其他人。您可以添加cmake输出吗?我怀疑CMake采用了旧的python版本。对于Python2和Python3,
    BOOST_PYTHON_模块
    的含义不同。在编译过程中,似乎构建了python 2变体。我执行了brew安装boost python——带-python3——如果没有python,cmake无法找到boost(它与brew安装boost python一起工作)+cmake如果以前安装过boost python,则找不到BoostClear缓存文件夹/重新启动MAC。缓存文件夹:\Library\Caches,否则cmake将无法定位到boost库。