我试图使用C扩展名文件构建共享库,但首先必须使用以下命令生成输出文件:

gcc -Wall utilsmodule.c -o Utilc

执行命令后,我收到以下错误消息:

> utilsmodule.c:1:20: fatal error: Python.h: No such file or directory
compilation terminated.

我已经在互联网上尝试了所有建议的解决方案,但问题仍然存在。我对Python.h没有问题。我设法在我的机器上找到了该文件。


当前回答

尝试apt文件。很难记住丢失文件所在的包名。它是通用的,适用于任何包文件。

例如:

root@ubuntu234:~/auto# apt-file search --regexp '/Python.h$'
pypy-dev: /usr/lib/pypy/include/Python.h
python2.7-dbg: /usr/include/python2.7_d/Python.h
python2.7-dev: /usr/include/python2.7/Python.h
python3.2-dbg: /usr/include/python3.2dmu/Python.h
python3.2-dev: /usr/include/python3.2mu/Python.h
root@ubuntu234:~/auto# 

现在你可以做出专家猜测,从中选择哪一个。

其他回答

经确认,如果您正在运行centos 8+。您需要运行以下操作:

sudo yum -y install python36 python38 python39
sudo yum -y install python36-devel.x86_64 python38-devel.x86_64 python39-devel.x86_64

pip3.8 install cpython
pip3.9 install cpython
pip3.6 install cpython

问题可能是您没有安装适当的Python库头。如果您使用Linux Solus,可以使用以下命令解决此问题:

sudo eopkg install python3-devel

特别是对于Python 3.7和Ubuntu,我需要

sudo apt安装libpython3.7-dev

.我想在某些时候,名字从pythom.n-dev改成了这个。

对于Python 3.6、3.8到3.10(以及计数…),类似地:

sudo apt安装libpython3.6-dev 

sudo apt安装libpython3.8-dev 

sudo apt安装libpython3.9-dev

sudo apt安装libpython3.10-dev

如果您在Amazon Linux上使用Python 3.6(基于RHEL,但此处给出的RHEL答案无效):

sudo yum install python36-devel

我设法解决了这个问题,并在一个命令中生成.so文件

gcc -shared -o UtilcS.so
-fPIC -I/usr/include/python2.7 -lpython2.7  utilsmodule.c