【问题标题】:Adding Python to C++: not finding Python.h将 Python 添加到 C++:找不到 Python.h
【发布时间】:2014-05-18 07:25:56
【问题描述】:

我想将一些用 Python 编写的函数添加到我的 C++ 程序中:

#include <iostream>
#include <Python.h>

using namespace std;

int main(){
    int a = 0;
    cout << a;
    return 0;
}

但是当我使用推荐 g++ main.cpp -Wall -o main 编译这个程序时 我有一个错误:fatal error: Python.h: No such file or directory

我试图通过sudo apt-get install python2.7-dev 安装python2.7 -dev 来解决问题,但它没有帮助。有人可以建议我还能做些什么来解决这个问题吗?

【问题讨论】:

    标签: python c++ python-2.7 ubuntu


    【解决方案1】:

    Additional note:

    由于 Python 可能会定义一些预处理器定义,这些定义会影响 某些系统上的标准头文件,您必须在前面包含Python.h 包括任何标准标题。

    【讨论】:

    • 这应该是一个评论而不是一个答案,因为它不能解决问题,而是试图在主要问题修复后阻止即将到来的附加问题。
    【解决方案2】:

    您需要告诉编译器在哪里可以找到 Python 标头。例如,在某些系统上,您会这样做:

    g++ -I /usr/include/python2.7 ...
    

    【讨论】:

    • 我做了g++ -I usr/include/python2.7/Python.h main.cpp -Wall -o main,但我仍然有同样的错误。我做错了吗?
    • 您已将 Python.h 添加到命令行。不要那样做,做我写的( ... 意味着你的 main.cpp 和其余部分)。阅读有关-I 选项的手册。
    • @Rop 你在那个路径上也缺少一个前导 /
    • @JohnZwinck 对于我的赞扬:g++ -I/usr/include/python2.7 -lpython2.7 main.cpp -Wall -o main 现在效果很好。
    猜你喜欢
    • 2016-09-17
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2014-10-20
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多