简体   繁体   中英

“Segmentation fault (core dumped)” Error in Boost.Python Hello_world example

For Installation, I have followed this procedure --> boost-py#installation-for-linux-ubuntu

Files:

  • 'CMakeLists.txt'
cmake_minimum_required(VERSION 3.0)

find_package(Boost COMPONENTS python36 required)
find_package(PythonInterp 3)
find_package(PythonLibs 3)

PYTHON_ADD_MODULE(hello hello.cpp)

include_directories(/usr/include/python3.6m)

FILE(COPY hello.py DESTINATION .)

add_test(NAME 01-HelloWorld COMMAND ${PYTHON_EXECUTABLE} hello.py)
  • 'hello.py'
#!/usr/bin/env python

import hello
print (hello.greet())
  • 'hello.cpp'
char const* greet()
{
   return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

Basically, I am following this repo: TNG/boost-python-examples

Building, Execution:

On running via bash terminal, I am getting "Segmentation Fault (core dumped)" on running $./hello.so or $ python hello.py

abhi3700@Abhijit:/mnt/f/Coding/github_repos/cpp-playground/gitcpplibs/boost-py-eg/01-HelloWorld$ cmake .
-- Boost  found.
-- Found Boost components:
   python36;required
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/f/Coding/github_repos/cpp-playground/gitcpplibs/boost-py-eg/01-HelloWorld
abhi3700@Abhijit:/mnt/f/Coding/github_repos/cpp-playground/gitcpplibs/boost-py-eg/01-HelloWorld$ make
[100%] Built target hello
abhi3700@Abhijit:/mnt/f/Coding/github_repos/cpp-playground/gitcpplibs/boost-py-eg/01-HelloWorld$ ./hello.so
Segmentation fault (core dumped)
abhi3700@Abhijit:/mnt/f/Coding/github_repos/cpp-playground/gitcpplibs/boost-py-eg/01-HelloWorld$ python3 hello.py
Segmentation fault (core dumped)
abhi3700@Abhijit:/mnt/f/Coding/github_repos/cpp-playground/gitcpplibs/boost-py-eg/01-HelloWorld$

I have been trying to resolve this error. I referred to many solutions, but couldn't solve my one. Can anyone help please?? It's really IMPORTANT......

THANKS!!

I found the solution: The new "CMakeLists.txt" is as follows:

cmake_minimum_required(VERSION 3.0)
 
project(greeter)

# Find necessary packages
find_package(PythonInterp 3)
find_package(PythonLibs 3 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIR})
 
# find_package(Boost COMPONENTS python3 REQUIRED)
find_package(Boost COMPONENTS python${PYTHON_VERSION_MAJOR})

include_directories(${Boost_INCLUDE_DIR})
 
# Build & Link our library
add_library(hello MODULE hello.cpp)

# setting Boost_LIBRARIES & PYTHON_LIBRARIES
if(UNIX)
        set(Boost_LIBRARIES "/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so.1.65.1")
        set(PYTHON_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpython3.6m.so")
endif()

target_link_libraries(hello ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

# don't prepend wrapper library name with lib
set_target_properties(hello PROPERTIES PREFIX "")

This gives the following output:

$ cmake .
$ make
$ python3 hello.py
hello, world

Actually, I identified the problem in the hello.so (NEW) file:

$ ldd hello.so
        linux-vdso.so.1 (0x00007fffda757000)
        libboost_python3-py36.so.1.65.1 => /usr/lib/x86_64-linux-gnu/libboost_python3-py36.so.1.65.1 (0x00007f2a52ec0000)
        libpython3.6m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0 (0x00007f2a52810000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f2a52480000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2a52260000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2a51e60000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2a51c40000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f2a519f0000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f2a517d0000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2a515c0000)
        libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f2a513b0000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f2a51010000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2a53400000)

So, it is missing libboost_python3-py36.so.1.65.1 ie boost_python, which was already installed in my system, via $ sudo apt-get install libboost-all-dev :

  • just locate this via:
$ ls /usr/lib/x86_64-linux-gnu | grep libboost_python3 | grep 1.65.1
libboost_python3-py36.so.1.65.1

NOTE: for 64-bit machine, it gets saved into /usr/lib/x86_64-linux-gnu , otherwise into /usr/lib folder.

Thanks @thatrobotguy & @squareskittles for looking into my issue. THANKS. once again..

A solution to build and run the above hello.cpp and hello.py without CMake and Make:

(1) Install Boost Python for Ubuntu sudo apt install libboost-all-dev

(2) Create hello.cpp and hello.py the same as in the question

(3) Find libboost_python3.so file, usually in /usr/lib/x86_64-linux-gnu

(4) Find Python include dir, for example 3.6 on Ubuntu: /usr/include/python3.6

(5) Build using GCC

g++ -shared -o hello.so hello.cpp -fPIC \
-I /usr/include/python3.6 -L /usr/lib/x86_64-linux-gnu -l:libboost_python3.so

(6) Run the hello.py file

python3 hello.py

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