簡體   English   中英

C 致命錯誤:Python.h:沒有這樣的文件或目錄#include<Python.h>

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

我在 C 中運行 Python,我似乎無法編譯程序。 我在程序中包含以下內容:

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

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

我在 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"
}

當我編譯時,我得到一個錯誤:

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語句,我已經包含了運行它所需的一切。 包含必要文件的最佳方式是什么? 順便說一句,我也遇到了#include <glib.h>錯誤,它應該包含在-I/usr/include/**中。 我對C很陌生,所以如果這是一個明顯的問題,我深表歉意。

我建議使用pkg-config

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

我將其發布為答案,但歸功於這里的幾個貢獻者。 訣竅是單獨包含您需要的每個文件夾。 所以我的新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