简体   繁体   English

C 致命错误:Python.h:没有这样的文件或目录#include<Python.h>

[英]C fatal error: Python.h: No such file or directory #include <Python.h>

I am running Python within C, and I can't seem to get the program to compile.我在 C 中运行 Python,我似乎无法编译程序。 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:我在 VS Code 中工作,我有以下tasks.json文件:

{
    "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.我认为使用-I语句,我已经包含了运行它所需的一切。 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/** .顺便说一句,我也遇到了#include <glib.h>错误,它应该包含在-I/usr/include/**中。 I'm pretty new to C so I apologize if this is an obvious question.我对C很陌生,所以如果这是一个明显的问题,我深表歉意。

I would suggest using pkg-config我建议使用pkg-config

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

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.json文件是:

{
    "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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM