Skip to main content
 首页 » 编程设计

python之无法在 OS X 上使用 boost-python 编译 python 绑定(bind)

2025年05月04日12over140

我尝试使用 boost-python 在 C++ 上进行 python 绑定(bind),但我无法编译,也找不到原因。

我正在使用来自 boost-python hello.cpp 的代码示例:

// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost 
// Software License, Version 1.0. (See accompanying 
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 
 
#include <boost/python/class.hpp> 
#include <boost/python/module.hpp> 
#include <boost/python/def.hpp> 
#include <iostream> 
#include <string> 
 
namespace { // Avoid cluttering the global namespace. 
 
  // A friendly class. 
  class hello 
  { 
    public: 
      hello(const std::string& country) { this->country = country; } 
      std::string greet() const { return "Hello from " + country; } 
    private: 
      std::string country; 
  }; 
 
  // A function taking a hello object as an argument. 
  std::string invite(const hello& w) { 
    return w.greet() + "! Please come soon!"; 
  } 
} 
 
BOOST_PYTHON_MODULE(extending) 
{ 
    using namespace boost::python; 
    class_<hello>("hello", init<std::string>()) 
        // Add a regular member function. 
        .def("greet", &hello::greet) 
        // Add invite() as a member of hello! 
        .def("invite", invite) 
        ; 
 
    // Also add invite() as a regular function to the module. 
    def("invite", invite); 
} 

使用这个Jamroot:

exe hello : hello.cpp ; 

当我运行 b2 --debug-configuration -n 时,一切似乎都很好:

$ ~/Sites/boost $ b2 --debug-configuration -n 
notice: found boost-build.jam at /usr/local/share/boost-build/boost-build.jam 
notice: loading Boost.Build from /usr/local/share/boost-build/kernel 
notice: Searching /etc /Users/jauneau /usr/local/share/boost-build/kernel /usr/share/boost-build /usr/local/share/boost-build/kernel /usr/local/share/boost-build/util /usr/local/share/boost-build/build /usr/local/share/boost-build/tools /usr/local/share/boost-build/contrib /usr/local/share/boost-build/. for site-config configuration file site-config.jam . 
notice: Configuration file site-config.jam not found in /etc /Users/jauneau /usr/local/share/boost-build/kernel /usr/share/boost-build /usr/local/share/boost-build/kernel /usr/local/share/boost-build/util /usr/local/share/boost-build/build /usr/local/share/boost-build/tools /usr/local/share/boost-build/contrib /usr/local/share/boost-build/. . 
notice: Searching /Users/jauneau /usr/local/share/boost-build/kernel /usr/share/boost-build /usr/local/share/boost-build/kernel /usr/local/share/boost-build/util /usr/local/share/boost-build/build /usr/local/share/boost-build/tools /usr/local/share/boost-build/contrib /usr/local/share/boost-build/. for user-config configuration file user-config.jam . 
notice: Loading user-config configuration file user-config.jam from /Users/jauneau/user-config.jam . 
notice: OSX version on this machine is 10.7.5 
notice: will use 'g++' for darwin, condition <toolset>darwin-4.2.1 
notice: using strip for <toolset>darwin-4.2.1 at /usr/bin/strip 
notice: using archiver for <toolset>darwin-4.2.1 at /usr/bin/libtool 
notice: available sdk for <toolset>darwin-4.2.1/<macosx-version>10.6 at /Developer/SDKs/MacOSX10.6.sdk 
notice: available sdk for <toolset>darwin-4.2.1/<macosx-version>10.7 at /Developer/SDKs/MacOSX10.7.sdk 
notice: [python-cfg] Configuring python... 
notice: [python-cfg]   user-specified version: "2.7" 
notice: [python-cfg] Checking interpreter command "python"... 
notice: [python-cfg] running command '"python" -c "from sys import *; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1' 
notice: [python-cfg] ...requested configuration matched! 
notice: [python-cfg] Details of this Python configuration: 
notice: [python-cfg]   interpreter command: "python" 
notice: [python-cfg]   include path: "/usr/local/bin/../Cellar/python/2.7.2/include/python2.7" 
notice: [python-cfg]   library path: "/usr/local/bin/../Cellar/python/2.7.2/lib/python2.7/config" "/usr/local/bin/../Cellar/python/2.7.2/lib" 
notice: [python-cfg] no framework directory found; using library path 
...found 13 targets... 
...updating 2 targets... 
darwin.compile.c++ bin/darwin-4.2.1/debug/hello.o 
 
    "g++"  -ftemplate-depth-128 -O0 -fno-inline -Wall -g -dynamic -no-cpp-precomp -gdwarf-2 -fexceptions -fPIC     -c -o "bin/darwin-4.2.1/debug/hello.o" "hello.cpp" 
 
darwin.link bin/darwin-4.2.1/debug/hello 
 
    "g++"  -o "bin/darwin-4.2.1/debug/hello" "bin/darwin-4.2.1/debug/hello.o"       -g  
 
 
...updated 2 targets... 

但是当我运行 b2 来编译我的绑定(bind)时,他找不到 python 头文件:

...found 13 targets... 
...updating 2 targets... 
darwin.compile.c++ bin/darwin-4.2.1/debug/hello.o 
In file included from /usr/local/include/boost/python/detail/prefix.hpp:13, 
                 from /usr/local/include/boost/python/class.hpp:8, 
                 from hello.cpp:5: 
/usr/local/include/boost/python/detail/wrap_python.hpp:50:23: error: pyconfig.h: No such file or directory 
/usr/local/include/boost/python/detail/wrap_python.hpp:75:24: error: patchlevel.h: No such file or directory 
/usr/local/include/boost/python/detail/wrap_python.hpp:78:2: error: #error Python 2.2 or higher is required for this version of Boost.Python. 
/usr/local/include/boost/python/detail/wrap_python.hpp:142:21: error: Python.h: No such file or directory 
In file included from /usr/local/include/boost/python/object/pointer_holder.hpp:14, 
                 from /usr/local/include/boost/python/to_python_indirect.hpp:10, 
                 from /usr/local/include/boost/python/converter/arg_to_python.hpp:10, 
                 from /usr/local/include/boost/python/call.hpp:15, 
                 from /usr/local/include/boost/python/object_core.hpp:14, 
                 from /usr/local/include/boost/python/object/class.hpp:9, 
                 from /usr/local/include/boost/python/class.hpp:13, 
                 from hello.cpp:5: 
/usr/local/include/boost/python/instance_holder.hpp:34: error: ‘PyObject’ has not been declared 
/usr/local/include/boost/python/instance_holder.hpp:41: error: expected ‘;’ before ‘(’ token 
/usr/local/include/boost/python/instance_holder.hpp:45: error: ‘PyObject’ has not been declared 
In file included from /usr/local/include/boost/python/object/pointer_holder.hpp:21, 
                 from /usr/local/include/boost/python/to_python_indirect.hpp:10, 
                 from /usr/local/include/boost/python/converter/arg_to_python.hpp:10, 
                 from /usr/local/include/boost/python/call.hpp:15, 
                 from /usr/local/include/boost/python/object_core.hpp:14, 
                 from /usr/local/include/boost/python/object/class.hpp:9, 
                 from /usr/local/include/boost/python/class.hpp:13, 
                 from hello.cpp:5: 
... 

我已经使用 Homebrew 安装了 boost 和 boost-build。

有什么想法吗?

请您参考如下方法:

这可能与您使用的是 Homebrew python 但 Boost 链接到系统 Python 的事实有关。