c++ boost python list extract causing Segmentation fault -


i running strange issue boost python. in trying unit test of code, have found after creating boost::python::list , appending information it, trying extract data causes seg fault. below simple example of problem.

main.cpp

#include <boost/python.hpp>  int main() {     boost::python::list testlist;     float = 1.12;     testlist.append(a);      // line results in seg fault     float b = boost::python::extract<float>(testlist[0]);     return 0; } 

and cmakelists.txt

cmake_minimum_required(version 2.8.3) project(boost_python_test)  find_package(boost required components system thread python) find_package(pythonlibs required)  if(boost_found)   set(boost_use_static_libs off)   set(boost_use_multithreaded on)   set(boost_use_static_runtime off) endif()  set(sources_directory ${project_source_dir}/src)  set(project_sources ${sources_directory}/main.cpp )   include_directories(${boost_include_dirs} ${python_include_dirs})  ## declare cpp executable add_executable(${project_name} ${sources_directory}/main.cpp )  target_link_libraries(${project_name} ${boost_libraries} ${python_libraries}) 

the compiler output is:

-- c compiler identification gnu 4.8.2 -- cxx compiler identification gnu 4.8.2 -- check working c compiler: /usr/bin/cc -- check working c compiler: /usr/bin/cc -- works -- detecting c compiler abi info -- detecting c compiler abi info - done -- check working cxx compiler: /usr/bin/c++ -- check working cxx compiler: /usr/bin/c++ -- works -- detecting cxx compiler abi info -- detecting cxx compiler abi info - done -- boost version: 1.54.0 -- found following boost libraries: --   system --   thread --   python -- found pythonlibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found     version "2.7.6")  -- configuring done -- generating done -- build files have been written to:         /home/jordan/git/boost_python_test/build 

you're going have use py_initialize work. check out embedding python. @eacousineau man , real brains behind solution.

#include <boost/python.hpp>  int main() {     // need line work     py_initialize();     boost::python::list testlist;     float = 1.12;     testlist.append(a);      // line no longer results in seg fault     float b = boost::python::extract<float>(testlist[0]);     return 0; } 

Comments