Can't compile python bindings using boost-python on OS X
Question: I try to do python bindings on C++ using boost-python, but I can't compile and
I can't find out why.

I'm using a code sample from 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);
    }
    

With this `Jamroot`:

    
    
    exe hello : hello.cpp ;
    

When I run `b2 --debug-configuration -n` everything seems fine:

    
    
    $ ~/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...
    

But when I run b2 to compile my bindings he can't find python header files:

    
    
    ...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:
    ...
    

I have installed both boost and boost-build using Homebrew.

Any ideas ?


Answer: it may be related to the fact that you are using a homebrew python but Boost
is linked to the system Python.

