由 locate 找到但不是由 GCC 找到的 Python.h

use*_*777 13 python c

我只是写了一个简单的 C 可执行文件来检查是否Python.h正常工作

#include<Python.h>
#include<stdio.h>
int main()
{
    printf("this is a python header file included programm\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

显然,它没有多大作用。但是,当我尝试使用gcc它进行编译时,出现错误:

#include<Python.h>
#include<stdio.h>
int main()
{
    printf("this is a python header file included programm\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后我检查了python-dev安装 python-dev包已Python.h安装或未使用locate.

foo.c:1:19: fatal error: Python.h: No such file or directory.
Run Code Online (Sandbox Code Playgroud)

我很清楚Python.h我的系统上有头文件。如何让我的可执行文件正常工作?

squ*_*org 20

您需要确认您的包含

#include <python2.7/Python.h>
Run Code Online (Sandbox Code Playgroud)

或者告诉 gcc 在哪里可以找到 Python.h

gcc -I /usr/include/python2.7/ program.c 
Run Code Online (Sandbox Code Playgroud)


Nat*_*man 9

您需要向 GCC 提供Python.h标头的包含路径。这可以通过-I标志来完成:

gcc -c -I/usr/include/python2.7 sourcefile.c

但是,还有更好的方法:使用pkg-config安装 pkg-config

pkg-config --cflags python

这将输出需要传递给 GCC 的标志,以便编译使用 Python 头文件和库的应用程序。

链接时,使用此命令的输出来包含适当的库:

pkg-config --libs python

您甚至可以将这两个步骤与:

gcc `pkg-config --cflags --libs python` sourcefile.c