在C ++编译文件中使用PyList_New时出现分段错误

时间:2019-06-02 20:01:38

标签: python c++ cpython

我试图在下面运行一个非常简单的程序(使用clang OSX进行编译):

//main.cpp
#include <Python.h>
#include <iostream.h>

int main()
{
  PyObject* listall = PyList_New(0);
  PyList_Append(listall, PyLong_FromLong(1));
  PyList_Append(listall, PyLong_FromLong(2));
  PyList_Append(listall, PyLong_FromLong(3));
  std::cout << listall << '\n'
}

并使用此makefile进行编译:

// CMakeLists.txt

cmake_minimum_required(VERSION 3.14.0)
project(arrow_test LANGUAGES CXX VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

add_executable(test main.cpp)

find_library(PYTHON_LIB python3.7m /Users/xx/miniconda3/envs/py-dev/lib/)

find_path(PYTHON_INCLUDE Python.h /Users/xx/miniconda3/envs/py-dev/include/python3.7m)

target_include_directories(a_test PUBLIC "${PYTHON_INCLUDE}")

target_link_libraries(a_test PUBLIC "${PYTHON_LIB}")

一切正常,但是我在PyObject* listall = PyList_New(0);上遇到段错误

并在调试器中运行,我得到:

Exception has occurred.
EXC_BAD_ACCESS (code=1, address=0x8)

感谢您的帮助。

0 个答案:

没有答案