3
我想遵循一些簡單的例子,使用libboost-python3將C++結構傳遞給Python。該功能按預期工作,但在退出時會出現段錯誤。Segfault與Boost 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
我是否需要使用REF計數或做我必須做一個明確的淨化步驟?我很努力地發現這個例子中的任何錯誤,因爲它很簡單。
而且附帶的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'是否有所作爲? –