【问题标题】:How to use python.h如何使用 python.h
【发布时间】:2020-05-12 07:45:10
【问题描述】:

我在我的程序中包含了 Python.h,当我编译程序时它显示多个错误,告诉“无法打开几个库” 错误信息如下

Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 7: Unable to open include file 'patchlevel.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 8: Unable to open include file 'pyconfig.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 9: Unable to open include file 'pymacconfig.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 50: Unable to open include file 'pyport.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 52: Unable to open include file 'pyatomic.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 63: Unable to open include file 'pymath.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 64: Unable to open include file 'pytime.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 65: Unable to open include file 'pymem.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 67: Unable to open include file 'object.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 68: Unable to open include file 'objimpl.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 69: Unable to open include file 'typeslots.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 71: Unable to open include file 'pydebug.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 73: Unable to open include file 'bytearrayobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 74: Unable to open include file 'bytesobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 75: Unable to open include file 'unicodeobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 76: Unable to open include file 'longobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 77: Unable to open include file 'longintrepr.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 78: Unable to open include file 'boolobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 79: Unable to open include file 'floatobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 80: Unable to open include file 'complexobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 81: Unable to open include file 'rangeobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 82: Unable to open include file 'memoryobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 83: Unable to open include file 'tupleobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 84: Unable to open include file 'listobject.h'
Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 85: Unable to open include file 'dictobject.h'
Error E2228 C:\Borland\bcc55\INCLUDE\Python.h 85: Too many error or warning messages
*** 26 errors in Compile ***

而且我的程序中没有包含任何上述缺失的库

#include<stdio.h>
#include<Python.h>
int main()
{
     Py_SetProgramName(argv[0]);  
     Py_Initialize();
     PyRun_SimpleString("print("hello")");
     Py_Finalize();
     return 0;
}

【问题讨论】:

  • 你是怎么编译的?似乎您只是在某个地方复制了 Python.h,但编译器没有使用正确的 include 目录。这些是编译器将在其中查找文件包含的标头的目录。
  • 消息说“无法打开包含文件”;没有提到图书馆。 (这是一个重要的区别,因为就编译器而言,库是链接器要处理的东西,而您并没有深入到编译过程中。)

标签: python c++ c python-3.x file


【解决方案1】:

错误信息

Error E2209 C:\Borland\bcc55\INCLUDE\Python.h 7: Unable to open include file 'patchlevel.h'

表示错误(无法打开文件)是由第 7 行中的文件 python.h 引入的。您可能没有包含所有这些头文件,但您的 python.h 可能。

然而,

  1. 尝试找出这些文件在您计算机上的存储位置。那么
  2. 了解如何告诉您的编译器在哪里搜索其他包含文件并提供您找到这些文件的位置。如果不这样做,编译器只会查看某些预配置的目录,在您的情况下,这些目录是不够的。

【讨论】:

    【解决方案2】:

    我的程序中没有包含上述任何缺失的库

    从技术上讲,你做到了。通过包含Python.h,您可以有效地包含它所包含和依赖的所有标题。如果这些不存在,那么您的代码将无法编译。您可以在Introduction Guide 中找到有关如何正确构建 CPython 的更多详细信息。

    【讨论】:

    • 是的,我会检查一下
    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2016-09-17
    • 1970-01-01
    • 2017-10-22
    • 2021-12-19
    相关资源
    最近更新 更多