简体   繁体   中英

Segmentation fault on execution of C++ code from Python – using Boost.Python on Mac OS

I am trying to write a library in C++ so that one could work with its methods from Python (especially jupyter notebooks) on different platforms. For this purpose I am using Boost.Python. I am debugging my code on my laptop with macOS Sierra 10.12.6.

I was able to compile my sources into a .so library, but regretfully its methods crash when I call them.

I installed Boost and Boost.Python libraries with the following sequence of commands:

brew install --build-from-source --with-python --fresh -vd boost
brew install boost-python

After that, I took the following simple C++ code ( main.cpp ):

#include <boost/python.hpp>
using namespace boost::python;
#include <string>

namespace {
    std::string greet() { 
        return "hello, world"; 
    }

    int square(int number) {
        return number * number;
    }
}

BOOST_PYTHON_MODULE(boost_python_example)
{
    def("greet", greet);
    def("square", square);
}

And created a CMakeLists.txt (cmake verison is 3.9.6):

cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Release)
if(APPLE)
    set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -lstdc++ -lpython2.7 -lboost_python")
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_library(boost_python_example SHARED main.cpp)
set_target_properties(boost_python_example PROPERTIES PREFIX "")

Then, by executing make clean && cmake . && make make clean && cmake . && make , I obtained the desired library boost_python_example.so and added the path to it to PYTHONPATH . Now my code can be successfully imported from Python, but it crashes in various ways on the execution of the methods:

user-osx1:BoostPythonTest user$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct  5 2017, 02:28:52)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boost_python_example
>>> from boost_python_example import *
>>> square(5)
Segmentation fault: 11

user-osx1:BoostPythonTest user$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct  5 2017, 02:28:52)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from boost_python_example import *
>>> greet()
Fatal Python error: PyEval_SaveThread: NULL tstate
Abort trap: 6

user-osx1:BoostPythonTest user$ which python
/Users/user/anaconda2/bin/python
user-osx1:BoostPythonTest user$ python --version
Python 2.7.14 :: Anaconda, Inc.

What am I doing wrong? As I understand, typically errors like this occur when Boost.Python is incompatible with the user's Python version. That is why I reinstalled Boost with --build-from-source --with-python options, but it did not help.

Does it missing PyEval_InitThreads() call from BOOST_PYTHON_MODULE block?

See: Why is PyGILState_Release throwing Fatal Python Errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM