【问题标题】:"Segmentation fault (core dumped)" Error in Boost.Python Hello_world exampleBoost.Python Hello_world 示例中的“分段错误(核心转储)”错误
【发布时间】:2020-08-08 21:03:11
【问题描述】:

对于安装,我遵循了这个程序 --> boost-py#installation-for-linux-ubuntu

文件:

  • '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);
}

基本上,我正在关注这个 repo:TNG/boost-python-examples

构建、执行:

通过bash 终端运行时,我在运行$ ./hello.so$ 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$

我一直在尝试解决此错误。我提到了许多解决方案,但无法解决我的一个。 有人可以帮忙吗?? 这真的很重要!!!...

谢谢!!

【问题讨论】:

  • 好吧,你不能运行.so 文件。这是一个问题。
  • 所以,我也尝试使用'python3 hello.py'。仍然,给出了同样的错误 - “分段错误”
  • 我看到你使用了char const*,这看起来不对...你的意思是const char*
  • 实际上,根据 C++ 标准,两者都是正确的......所以,这不是原因......
  • 实际上,我尝试链接该链接,但出现错误....我已经在几个小时前解决了这个问题....我会发布答案。

标签: c++ python-3.x boost cmake boost-python


【解决方案1】:

我找到了解决方案: 新的“CMakeLists.txt”如下:

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 "")

这给出了以下输出:

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

实际上,我在hello.so(新)文件中发现了问题:

$ 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)

所以,它缺少 libboost_python3-py36.so.1.65.1 即 boost_python,它已经通过 $ sudo apt-get install libboost-all-dev 安装在我的系统中:

  • 只需通过以下方式找到它:
$ ls /usr/lib/x86_64-linux-gnu | grep libboost_python3 | grep 1.65.1
libboost_python3-py36.so.1.65.1

注意:对于 64 位机器,它被保存到 /usr/lib/x86_64-linux-gnu,否则保存到 /usr/lib 文件夹中。

感谢@thatrobotguy 和@squareskittles 调查我的问题。谢谢!再一次..

【讨论】:

  • tks 的答案,它的工作原理!但是有没有关于仅使用 gcc 而不是 cmake & make 构建的指南?
  • 你必须在 I 标志中包含目录,例如 gcc test.cpp -I &lt;include directory&gt;
【解决方案2】:

在没有 CMake 和 Make 的情况下构建和运行上述 hello.cpphello.py 的解决方案:

(1) 为 Ubuntu 安装 Boost Python sudo apt install libboost-all-dev

(2) 创建hello.cpphello.py 与问题相同

(3) 查找libboost_python3.so文件,一般在/usr/lib/x86_64-linux-gnu

(4) 查找 Python 包含目录,例如 Ubuntu 上的 3.6:/usr/include/python3.6

(5) 使用 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) 运行hello.py文件

python3 hello.py

【讨论】:

    猜你喜欢
    • 2016-09-28
    • 2014-08-04
    • 2012-11-19
    • 1970-01-01
    • 2015-06-25
    • 2021-06-03
    相关资源
    最近更新 更多