g++ - Python.h:没有这样的文件或目录
g++ - Python.h: No such file or directory
我正在尝试制作一个C++脚本来运行一些简单的Python代码:
// t.cpp
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
运行g++ t.cpp
时,出现错误:
t.cpp:1:20:致命错误:Python.h:没有这样的文件或目录
编译已终止
我发现了许多类似的问题,都是特定于IDE或其他开发软件的,或者通过安装python3-dev
来解决。python3-dev
包已经安装,我什至尝试在尝试编译时手动包含标头:
g++ t.cpp -I ~/.virtualenvs/MainEnv/include/python3.5m/Python.h
g++ t.cpp -I /usr/include/python3.5m/Python.h
两者都没有改变任何东西。
如何修复此错误?
更新:我发现使用g++ t.cpp -I /usr/include/python3.5/
似乎包含标头,但随后它遇到了更多错误:
t.cpp:(.text+0x10(:对"Py_Initialize"的未定义引用
t.cpp:(.text+0x1f(:对"PyRun_SimpleStringFlags"的未定义引用
t.cpp:(.text+0x24(:对"Py_Finalize"的未定义引用
collect2:错误:ld 返回 1 个退出状态
我已经在我的github上设置了一个类似的例子
g++ t.cpp
缺少一些东西:
- 告诉 g++ cpython 的标头在哪里(通过
-I/path/to/headers/
( - 告诉 g++ 链接到 libpython (通过
-lpython3.5m
(
您还可以使用pkg-config
检索这些标志
$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m
您的命令行应如下所示g++ -I/usr/include/python3.5m t.cpp -lpython3.5m
#include <...>
适用于编译器附带的包含。
将#include "Python.h"
用于任何其他包含。
运行以下命令来编译代码:
我的测试.cpp:
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
编译:
$ g++ mytest.cpp `pkg-config python3-embed --libs --cflags` -o mytest
$ ./mytest
相关文章:
- 是否可以使用 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>