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

gcc -Wall utilsmodule.c -o Utilc

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

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

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


当前回答

若您使用cmake构建项目,可以使用这个示例。

cmake_minimum_required(VERSION 2.6)
project(demo)
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(demo main.cpp)
target_link_libraries(demo ${PYTHON_LIBRARIES})

其他回答

对于OpenSuse的同志们:

sudo zypper install python3-devel

CentOS 7:

sudo yum install python36u-devel

我按照这里的说明在几个虚拟机上安装python3.6:https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-centos-7然后能够构建mod_wsgi并使其与python3.6 virtualenv一起工作

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

sudo eopkg install python3-devel

这不是同一种情况,但它也适用于我,现在我可以在Python3.5中使用SWIG:

我试图编译:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/

使用Python 2.7可以很好地工作,而不是使用我的3.5版本:

exist_wrap.c:147:21:致命错误:Python.h:不存在el archivo o eldirectorio编译已终止。

在Ubuntu 16.04安装中运行后:

sudo apt-get install python3-dev  # for python3.x installs

现在我可以毫无问题地编译Python3.5:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/

当我尝试在CentOS 7上使用Python3.6安装ctds时发生此错误。我完成了这里提到的所有技巧,包括yum安装python34-devel。问题是Python.h在/usr/include/python3.4m中找到,但在/usr/include/python3.6m中找不到。我尝试使用--global选项指向include-dir(pip3.6 install--global option=build_ext--global options=“--include-dirs=/usr/include/pyton3.4m”ctds)。这导致在链接ctd时找不到lpython3.6m。

最后,工作是修复Python3.6的开发环境,需要使用include和libs进行纠正。

yum -y install https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/python36u-libs-3.6.3-1.ius.centos7.x86_64.rpm

Python.h需要位于gcc的include路径中。无论使用哪种版本的python,例如,如果它是3.6,那么它通常应该在/usr/include/python3.6m/python.h中。