c++ boost python列表提取导致分段错误
c++ boost python list extract causing Segmentation fault
我遇到了boost python的一个奇怪问题。在试图对我的一些代码进行单元测试时,我发现在创建boost::python::列表并向其添加信息后,试图提取该数据会导致segfault。下面是我的问题的一个简单例子。
main.cpp
#include <boost/python.hpp>
int main()
{
boost::python::list testList;
float a = 1.12;
testList.append(a);
// This line results in a seg fault
float b = boost::python::extract<float>(testList[0]);
return 0;
}
和我的CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(boost_python_test)
find_package(Boost REQUIRED COMPONENTS system thread python)
find_package(PythonLibs REQUIRED)
IF(Boost_FOUND)
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_RUNTIME OFF)
ENDIF()
set(SOURCES_DIRECTORY ${PROJECT_SOURCE_DIR}/src)
set(PROJECT_SOURCES
${SOURCES_DIRECTORY}/main.cpp
)
include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
## Declare a cpp executable
add_executable(${PROJECT_NAME}
${SOURCES_DIRECTORY}/main.cpp
)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
编译器输出为:
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.54.0
-- Found the following Boost libraries:
-- system
-- thread
-- python
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found version "2.7.6")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jordan/Git/boost_python_test/build
您必须使用Py_Initialize才能使其工作。看看嵌入Python。@eacousineau是这个解决方案背后的男人和真正的大脑。
#include <boost/python.hpp>
int main()
{
// Need this line for it to work
Py_Initialize();
boost::python::list testList;
float a = 1.12;
testList.append(a);
// This line no longer results in a seg fault
float b = boost::python::extract<float>(testList[0]);
return 0;
}
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?