Home > Mobile >  C fatal error: Python.h: No such file or directory #include <Python.h>
C fatal error: Python.h: No such file or directory #include <Python.h>

Time:06-14

I am running Python within C, and I can't seem to get the program to compile. I have the following included in the program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include <Python.h>
#include <arrayobject.h>

I am working in VS Code and I have the following tasks.json file:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C  : gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-I/usr/include/**",
                "-I~/anaconda3/envs/myenv/include/python3.9/**",
                "-I~/anaconda3/envs/myenv/lib/python3.9/site-packages/numpy/core/include/numpy/**",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

When I compile, I get an error:

Starting build...
/usr/bin/gcc -fdiagnostics-color=always -I/usr/include/** -I~/anaconda3/envs/myenv/include/python3.9/** -I~/anaconda3/envs/myenv/lib/python3.9/site-packages/numpy/core/include/numpy/** -g ~/Documents/code/test.c -o ~/Documents/code/test
~/Documents/code/test.c:6:10: fatal error: Python.h: No such file or directory
    6 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.

Build finished with error(s).

I thought that with the -I statements, I'd included everything necessary to run this. What is the best way to include the necessary files? By the way, I also get errors with #include <glib.h>, which should be included with -I/usr/include/**. I'm pretty new to C so I apologize if this is an obvious question.

CodePudding user response:

I would suggest using pkg-config

"args": [
  "pkg-config --cflags python3",
}

CodePudding user response:

I am posting this as an answer, though credit goes to the several contributors here. The trick is to include each folder you need individually. So my new tasks.json file is:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C  : gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [

                "-fdiagnostics-color=always",
                "-I/usr/include/glib-2.0",
                "-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "-I$HOME/anaconda3/envs/myenv/include/python3.9",
                "-I$HOME/anaconda3/envs/myenv/lib/python3.9/site-packages/numpy/core/include/numpy",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
  • Related