[C++-sig] Boost python extract on Mac OS Mavericks - seg fault

Brian Bruggeman brian.m.bruggeman at gmail.com
Fri Aug 29 00:19:07 CEST 2014


Hi all,

I am having trouble with a really simple example, and I am hoping someone on this list can help me.

I have created a github repo for code in question:

    git clone git at github.com:brianbruggeman/boost_python_hello_world.git

I have also created a stack overflow question:

    http://stackoverflow.com/q/25533329/631199

The basic problem is as follows:

    Currently, I am able to compile cleanly, but when executing the code, I receive a segmentation fault. I've narrowed the seg-fault down to the line which actually uses boost::python::extract.

Code:

    #include <boost/python.hpp>
    #include <iostream>

    namespace bp = boost::python;

    // Embedding python
    int main(int argc, char** argv) {
        int data = 0;
        Py_Initialize();
        PyRun_SimpleString("data = 1");
        bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__"))));
        bp::object dictionary = module.attr("__dict__");
        bp::object data_obj = dictionary["data"];
        // Error: The following line has the segmentation fault...
        data = bp::extract<int>(data_obj);
        std::cout << "data = " << data << std::endl;
        Py_Finalize();
        return 0;
    }

CMakeLists.txt:

    project(hello)
    cmake_minimum_required(VERSION 2.8)

    FIND_PACKAGE(PythonInterp)
    FIND_PACKAGE(PythonLibs)
    FIND_PACKAGE(Boost COMPONENTS python)

    include_directories(${PYTHON_INCLUDE_DIRS} ${Boost_INCLUD_DIRS})
    link_directories(${PYTHON_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS})

    add_executable(hello say_hello.cpp)
    target_link_libraries(hello
      ${Boost_LIBRARIES}
      ${PYTHON_LIBRARIES})

Also for completeness…

I installed Python and Boost::Python using the following…

    brew install python
    brew install boost —with-python

Thanks so much in advance!

Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20140828/806de42f/attachment.html>


More information about the Cplusplus-sig mailing list