Python.h:没有这样的文件或目录
Python.h: No such file or directory
我最近在运行Ubuntu 12.04 LTS的Macbook Pro上安装了KDevelop 4用于C++开发。
我想在我的C++代码中嵌入 Python 应用程序。为此,需要包含Python.h
头文件。所以,我做到了。
#include <iostream>
#include <Python.h>
int main(int argc, char **argv) {
Py_Initialize();
return 0;
}
但是,在运行时,我收到了来自 IDE 的以下响应:
fatal error: Python.h: No such file or directory
我已经安装了python-dev
软件包。
所以,我认为一定是 KDevelop 未包含头文件的问题。因此,我将相关文件夹添加到包含路径中,KDevelop 通过删除上面代码中第二个包含语句下方的红色下划线立即识别出了这一点。
但是,问题仍然存在。我得到同样的错误。感谢您可以提供的任何帮助或意见:-)
(KDevelop 正在为我的项目使用 CMake。
在 CMakeLists.txt 中,尝试添加以下内容:
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(<your exe or lib> ${PYTHON_LIBRARIES})
有关命令的详细信息,请运行:
cmake --help-module FindPythonLibs
cmake --help-command find_package
cmake --help-command include_directories
cmake --help-command target_link_libraries
sudo apt-get install pythonX.X-dev
例如,对于 3.8
sudo apt-get install python3.8-dev
谢谢克里斯蒂安js19的评论。
原答案:
sudo apt-get install python2.7-dev
为我解决了"Python.h:没有这样的文件或目录"问题
您希望在编译行中包含以下内容:
`python-config --cflags`
这在链接行上:
`python-config --ldflags`
Python.h
不在构建系统的包含路径中。你可以通过运行来找出你的Python.h在哪里
dpkg -L python-dev | grep Python.h
这也将验证python-dev包是否实际安装了Python.h。
我这里没有kdevelop,但是大多数IDE都有一个设置,您可以在其中指定构建系统使用的包含路径,并且您应该能够添加Python.h所在的路径。
编辑:
正如 Nikolai 所暗示的那样,您还需要为链接阶段添加正确的库路径。(python-config --ldflags的输出)。
对于 Linux Ubuntu 腻子用户,请尝试以下操作:
sudo apt-get update
sudo apt-get install python-dev
然后编译它
gcc -o check xyz.c -I/usr/include/python2.7/ -lpython2.7
然后运行它
./check
我假设它已经安装好了。查找路径:
find / -iname python.h
当你这样做时,在编译添加
-I python_h_path
- 是否可以使用 libclang python 解析 cpp 文件中没有标头的函数原型
- 为什么我无法打开/读取从 Python 调用的 C 扩展名中的 txt 文件?
- 从 Python 调用 c++ 文件
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 访问 Python 内部的C++文件
- NS3 - python.h 文件无法定位编译错误
- 如何将二进制文件头示例代码 (C++/C#) 转换为 Python?
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 将数据从 c++ 文件连续发送到 Python 脚本以进行进一步处理
- 在使用PyInstaller冻结Python脚本时包含一个C++可执行文件
- OpenCV - 打开一个从C++到Python的XML文件
- Python 脚本到 c .obj 文件
- 提升 Python 找不到 dll 文件
- 尝试在 python 中正确加载和使用 dll 文件
- g++ - Python.h:没有这样的文件或目录
- Python API用于解析包含数组格式的protobuf数据的二进制文件
- 如何使用c ++代码来调用和运行一个现有的python文件?
- 在 Python/C++ 中用更改文件中的随机数替换数字
- 如何设置cmakelists.txt文件以启动和运行Xtensor-python示例代码
- 找不到文件 <Python.h>