使用Homebrew在Mac OS X上构建python-boost hello world - Makefile与Resolution

时间:2016-08-04 23:00:31

标签: python c++ macos makefile boost-python

虽然不直接是一个明确的问题 - 我正在尝试撰写一篇文章,解决在尝试安装boost-python作为新手时遇到的一些奇怪问题

该帖子基于this example,可直接通过

获取
wget https://dl.dropboxusercontent.com/u/508241/wordpress/BoostPythonHelloWorld.tar.gz
tar xf BoostPythonHelloWorld.tar.gz
cd BoostPythonHelloWorld
cmake .
make
./test.py

一个大胆但准确的说法是,我已经系统地阅读了{em>所有相关的"boost-python hello world Makefile"(没有那么多)谷歌搜索结果,并通过每个SO帖子出现在搜索boost-python hello world Makefile。我尝试过的方法显然相当详尽,我也尝试过大量的例子。

问题

我在Makefile的最佳成功是在这篇文章的最后以便于阅读。这些问题主要基于此。

  • 第一个是brew install boost-python将在/usr/Cellar/boost/1.61.0/之类的地方安装提升,这可能导致大多数在线资源失败,除非明确链接
  • Mac OS X附带原生python。大多数用户都会安装homebrew install python,这会导致cmake错误地链接到本机python。
  • 我们必须明确链接pythonlibpython2.7.dylib
  • 以下Makefile的某些组合实际编译为100%但后来我有以下内容,似乎来自boost-python无法链接到{{ 1}}:

    cpython

    我更改了我的 [ 25%] Building CXX object CMakeFiles/greet.dir/greet.cpp.o [ 50%] Linking CXX shared library libgreet.dylib [ 50%] Built target greet [ 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: "_PyArg_ParseTupleAndKeywords", referenced from: 而没有清理我的Makefile几次,当我意识到我忘记清除旧文件时​​,我已经更改了Build/太多来重新创建这个成功的程度

  • 删除Makefile编译时没有错误,但我无法导入,因为我应该可以使用SHARED

import greet_ext

Makefile

project( BoostPythonHelloWorld ) cmake_minimum_required(VERSION 3.3) set(Boost_REALPATH ON) set(Boost_USE_STATIC_LIBS ON) set(Boost_USE_MULTITHREADED ON) find_package(Boost COMPONENTS regex filesystem system thread python chrono date_time atomic REQUIRED) # include extras message("") message("CMAKE finds wrong dirs of Boost (Mac OSX default)...") message("... Include Include of boost: " ${Boost_INCLUDE_DIRS} ) set(Boost_INCLUDE_DIRS "/usr/Cellar/boost/1.61.0/") set(Boost_INCLUDE_DIRS "/usr/Cellar/boost/1.61.0/include") message("... Actual Include of boost: " ${Boost_INCLUDE_DIRS}} ) find_package(PythonLibs 2.7 REQUIRED) message("") message("CMAKE finds wrong dirs of Python (Mac OSX default)...") message("... Include dirs of Python: " ${PYTHON_INCLUDE_DIRS} ) message("... Libs of Python: " ${PYTHON_LIBRARIES} ) set(PYTHON_LIBRARIES "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib") set(PYTHON_INCLUDE_DIRS "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/include/python2.7") message("... Actual Include: " ${PYTHON_INCLUDE_DIRS} ) message("... Actual lib: " ${PYTHON_LIBRARIES} ) # Build our library add_library( greet SHARED greet.cpp ) # Define the wrapper library that wraps our library add_library( greet_ext SHARED greet_ext.cpp ) target_link_libraries( greet_ext ${Boost_LIBRARIES} greet ) # don't prepend wrapper library name with lib set_target_properties( greet_ext PROPERTIES PREFIX "" ) 输出

使用命令:Makefile在我获得的目录rm -rf Build; mkdir Build; cd Build; cmake ..; make;中:

BoostPythonHelloWorld

1 个答案:

答案 0 :(得分:1)

这并没有直接解决CMAKE finds wrong dirs of Boost (Mac OSX default)... ... Include Include of boost: /usr/local/include ... Actual Include of boost: /usr/Cellar/boost/1.61.0/include} -- Found PythonLibs: /usr/lib/libpython2.7.dylib (found suitable version "2.7.10", minimum required is "2.7") CMAKE finds wrong dirs of Python (Mac OSX default)... ... Include dirs of Python: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/python2.7 ... Libs of Python: /usr/lib/libpython2.7.dylib ... Actual Include: /usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/include/python2.7 ... Actual lib: /usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib -- Configuring done -- Generating done -- Build files have been written to: /Users/alex/Downloads/BoostPythonHelloWorld/Build Scanning dependencies of target greet [ 25%] Building CXX object CMakeFiles/greet.dir/greet.cpp.o [ 50%] Linking CXX shared library libgreet.dylib clang: error: invalid argument '-bundle' not allowed with '-dynamiclib' make[2]: *** [libgreet.dylib] Error 1 make[1]: *** [CMakeFiles/greet.dir/all] Error 2 make: *** [all] Error 2 问题,而是让其他python用户意识到我的突破,Makefile没有必要。

我仍会接受任何导致Makefile尝试修复的答案。

使用其他示例Makefile

hello_ext.cpp

我可以使用从this post获取的char const* greet() { return "hello, world"; } #include <boost/python.hpp> BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); } 将其导入到python中:

setup.py

建立:from distutils.core import setup from distutils.extension import Extension hello_ext = Extension( 'hello_ext' ,sources=['hello_ext.cpp'] ,libraries=['boost_python-mt'] # for python 3 use 'boost_python3-mt' # you may also want to add these ,extra_compile_args=['-std=c++11','-stdlib=libc++'] ,extra_link_args=['-stdlib=libc++'] ) setup( name='hello-world', version='0.1', ext_modules=[hello_ext]) 我能够成功导入!

python setup.py build_ext --inplace

作为旁注:以下内容属于In [1]: ls Makefile build/ hello_ext.cpp hello_ext.so* setup.py import hello_ext hello_ext.greet() ## -- End pasted text -- Out[2]: 'hello, world'

~/.bash_profile