0

I'm working on a Python 2.7 project and I'm attempting to write a c++ extension to speed up our implementation of the D* lite algorithm. Never played with python C API, I'm struggling a lot in setting up my environment. To check whether I can access the C API, I wrote this simple snippet, but unfortunately I cannot manage to get it compiled properly.

Here's the snippet, aka dstar_lite.cpp

#include <Python.h>
#include "numpy/arrayobject.h"

int main(int argc, char *argv[]) {
    if (PyErr_Occurred()) {
        printf("Error\n");
        return -1; // Or some suitable return value to indicate failure.
    }
    return 0;
}

My IDE is visual studio code, and these are my c_cpp_properties.json and my tasks.json configuration files:

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/usr/include/python2.7/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-I/usr/include/python2.7",
                "-I/usr/include/x86_64-linux-gnu/python2.7",
                "-fno-strict-aliasing",
                "-Wdate-time",
                "-D_FORTIFY_SOURCE=2",
                "-fdebug-prefix-map=/build/python2.7-QDqKfA/python2.7-2.7.18=.",
                "-fstack-protector-strong",
                "-Wformat",
                "-Werror=format-security",
                "-DNDEBUG",
                "-fwrapv",
                "-Wstrict-prototypes",
                "-L/usr/lib/python2.7/config-x86_64-linux-gnu",
                "-L/usr/lib",
                "-lpython2.7",
                "-lpthread",
                "-ldl",
                "-lutil",
                "-lm",
                "-Xlinker",
                "-export-dynamic",
                "-Wl,-O1",
                "-Wl,-Bsymbolic-functions",
                "-fPIC",
                "-fno-common",
                "-O2",
                "-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION",
                "-ggdb",
                "-Wall",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Here, includes, cflags and ldflags are taken by running the following commands, as suggested in several other threads here in StackOverflow:

python2-config --includes
> -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7
python2-config --cflags
> -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7  -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-QDqKfA/python2.7-2.7.18=. -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
python2-config --ldflags
> -L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -lpython2.7 -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

The error I get should be related to the linking process, to the best of my knowledge. I'm gonna share the complete log trace:

Starting build...
/usr/bin/gcc -fdiagnostics-color=always -g -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7 -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -fdebug-prefix-map=/build/python2.7-QDqKfA/python2.7-2.7.18=. -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG -fwrapv -Wstrict-prototypes -L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -L/usr/lib/python2.7/dist-packages/numpy/core/lib -lpython2.7 -lpthread -ldl -lutil -lm -lnpymath -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions -fPIC -fno-common -O2 -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -ggdb -Wall /home/david/git/AI4A_project/planning/dstar/dstar_lite.cpp -o /home/david/git/AI4A_project/planning/dstar/dstar_lite
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
In file included from /usr/include/python2.7/numpy/ndarrayobject.h:21,
                 from /usr/include/python2.7/numpy/arrayobject.h:4,
                 from /home/david/git/AI4A_project/planning/dstar/dstar_lite.cpp:63:
/usr/include/python2.7/numpy/__multiarray_api.h:1463:1: warning: ‘int _import_array()’ defined but not used [-Wunused-function]
 1463 | _import_array(void)
      | ^~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccJwaN2x.o: in function `main':
/home/david/git/AI4A_project/planning/dstar/dstar_lite.cpp:67: undefined reference to `PyErr_Occurred'
collect2: error: ld returned 1 exit status

Build finished with error(s).
12
  • In which header file is PyErr_Occurred defined? Commented Jan 19, 2022 at 10:44
  • /usr/include/python2.7/pyerrors.h Commented Jan 19, 2022 at 10:46
  • So is this header file included in some other file like Python.h or do you need to include it explicitly (you can try this BTW)? Commented Jan 19, 2022 at 10:48
  • 1
    The segmentation fault is because you have not called Py_Initialize(). You cannot use most of the Python C API until you do that Commented Jan 19, 2022 at 11:27
  • 1
    The linker fails, because cpp on the command line comes after libraries. It should be the other way around. See a similar post for a more detailed explanation: stackoverflow.com/a/48836737/5769463 Commented Jan 19, 2022 at 12:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.