1

I'm trying to run a very simple program below (compiling using 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'
}

And compiling using this 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}")

It complies fine, However I get a seg fault on PyObject* listall = PyList_New(0);

And running in the debugger I get:

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

Any help is appreciated.

clery00
  • 251
  • 2
  • 14
  • I'm not doing anything with ctypes. I've not enough experience with CPython to know if it's revelant, so I've tried it and I get Fatal Python error: `PyMUTEX_LOCK(_PyRuntime.ceval.gil.mutex)` failed. – clery00 Jun 02 '19 at 20:20
  • It doesn't look like you actually initialized Python. – user2357112 Jun 03 '19 at 09:09

0 Answers0