我正在尝试使用 C 扩展文件构建共享库,但首先我必须使用以下命令生成输出文件:
gcc -Wall utilsmodule.c -o Utilc执行命令后,我收到此错误消息:
utilsmodule.c:1:20:致命错误:Python.h:没有这样的文件或目录编译终止。
事实上我已经通过互联网尝试了所有建议的解决方案,但问题仍然存在... 我也没有Python.h问题。我设法在我的机器上找到该文件...... 任何人在遇到同样的问题之前?
看起来你没有正确安装 python dev 的头文件和静态库。使用包管理器在系统范围内安装它们。
对于apt ( Ubuntu,Debian ...... ):
sudo apt-get install python-dev # for python2.x installs
sudo apt-get install python3-dev # for python3.x installs对于yum ( CentOS,RHEL ...... ):
sudo yum install python-devel # for python2.x installs
sudo yum install python34-devel # for python3.4 installs对于dnf ( Fedora ...... ):
sudo dnf install python2-devel # for python2.x installs
sudo dnf install python3-devel # for python3.x installs对于zypper ( openSUSE ... ):
sudo zypper in python-devel # for python2.x installs
sudo zypper in python3-devel # for python3.x installs对于apk ( 阿尔卑斯山... ):
# This is a departure from the normal Alpine naming
# scheme, which uses py2- and py3- prefixes
sudo apk add python2-dev # for python2.x installs
sudo apk add python3-dev # for python3.x installs对于apt-cyg ( Cygwin ...... ):
apt-cyg install python-devel # for python2.x installs
apt-cyg install python3-devel # for python3.x installs
在 Ubuntu 上,我运行的是 Python 3,我不得不安装
sudo apt-get install python3-dev如果要使用未链接到 python3 的 Python 版本,请安装相关的 python3.x-dev 软件包。例如:
sudo apt-get install python3.5-dev
你需要做的两件事。
安装 Python 的开发包,如果是 Debian / Ubuntu / Mint,则使用命令完成:
sudo apt-get install python-dev第二件事是包含文件默认情况下不包含在 include 路径中,默认情况下 Python 库也不与可执行文件链接。您需要添加这些标志(相应地替换 Python 的版本):
-I/usr/include/python2.7 -lpython2.7换句话说,你的编译命令应该是:
gcc -Wall -I/usr/include/python2.7 -lpython2.7 utilsmodule.c -o Utilc