【问题标题】:Segfault with Boost Python on cleanup使用 Boost Python 进行 Segfault 清理
【发布时间】:2015-12-15 00:23:11
【问题描述】:

我正在尝试使用 libboost-python3 将 C++ 结构传递给 Python 的一些简单示例。该功能按预期工作,但在退出时会出现段错误。

我已将其提炼为最简单的示例,当创建的对象被释放时,我仍然会遇到段错误。

/* boost_python_exemplar.cpp: */

#include <boost/python.hpp>

struct Test_Struct
{
  int a;
};

BOOST_PYTHON_MODULE(libboost_python_exemplar)
{
  using namespace boost::python;

  class_<Test_Struct>("Test_Struct")
    .def_readwrite("a", &Test_Struct::a);
}

然后是附带的python代码:

# test.py

import libboost_python_exemplar
d = libboost_python_exemplar.Test_Struct()
# Segfault occurs here when the import is being cleaned up

我需要使用引用计数还是必须执行明确的清理步骤?我很难发现这个例子有什么问题,因为它很简单。

还有随附的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.3)
project(Boost_Python_Exemplar)

SET(Boost_INCLUDE_DIR /usr/local/boost/1.55.0/include/)
SET(Boost_LIBRARY_DIR /usr/local/boost/1.55.0/lib64/)
FIND_PACKAGE(Boost 1.55)
IF(Boost_FOUND)
  INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "/usr/local/anaconda_py3/include/python3.4m/")
  SET(Boost_USE_STATIC_LIBS_OFF)
  SET(Boost_USE_MULTITHREADED ON)
  SET(Boost_USE_STATIC_RUNTIME OFF)
  FIND_PACKAGE(Boost 1.55 COMPONENTS python3 REQUIRED)

  ADD_LIBRARY(boost_python_exemplar SHARED boost_python_exemplar.cpp)
  TARGET_LINK_LIBRARIES(boost_python_exemplar ${Boost_LIBRARIES})
ELSE()
  MESSAGE(FATAL_ERROR "Unable to find correct Boost version.")
ENDIF()

IF(CMAKE_COMPILER_IS_GNUCXX)
  ADD_DEFINITIONS("-Wall" "-pedantic" "-g")
ELSE()
  MESSAGE(FATAL_ERROR "CMakeLists.txt requires GCC")
ENDIF()

【问题讨论】:

  • 为最少的代码点赞,但是您也可以从构建过程中提取使用的命令吗?另外,anaconda_py3 有影响吗?

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


【解决方案1】:

Here 有人遇到了同样的错误。他正在使用带有 g++ 编译器的 ld 链接器。使用 g++ 链接器解决了他的问题。 您应该在 CMakeFiles/boost_python_exemplar.dir/link.txt 中检查您的链接器。

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2017-10-15
    • 2018-01-13
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多