Pygame 找不到包含文件 "sdl.h

5 投票
2 回答
4795 浏览
提问于 2025-04-15 11:28

我正在尝试在Windows上构建一个下载的Python应用程序,使用的是Pygame。我已经安装了Python 2.5和Pygame 1.7.1。我对Python还不太熟悉,但我试着在Windows的命令行中输入顶层的.py文件名。(我使用的是Win XP专业版。)

这是我收到的消息。

C:\Python25\include\pygame\pygame.h(68) : fatal error C1083: 无法打开包含文件:'SDL.h':没有这样的文件或目录

我以为Pygame是建立在SDL之上的,所以不需要单独安装SDL。不过,我还是安装了SDL 1.2.13,并把SDL的包含文件夹添加到了我的%INCLUDE%环境变量中。结果还是不行。

我注意到C:\Python25\Lib\site-packages\pygame里有几个SDL*.DLL文件,但在Python的文件结构中找不到sdl.h这个头文件。当然,我可以把sdl的头文件复制到C:\Python25\include\pygame文件夹里,但这样做我觉得不太好。

有没有人知道正确的设置方法?

编辑:这个应用程序是“企鹅机器” Pygame应用

2 个回答

2

当你编译一个程序时,编译器会在几个文件夹里查找头文件,这些文件夹有些是固定的,有些是你在编译时指定的,比如你可以用命令“gcc -I/usr/local/include ...”来告诉编译器去哪个文件夹找文件。可能你缺少了这些指定的路径。如果不是,那就去看看其他可能导致你出现的错误信息。

你需要安装 SDL 开发库,但你提到“我可以复制sdl头文件”,这听起来你已经有这些文件了。那么你现在的问题就是要让编译器去你放这些文件的文件夹里查找。

5

我在我的Linux电脑上尝试编译,结果遇到了同样的错误:

$ python setup.py build
DBG> include = ['/usr/include', '/usr/include/python2.6', '/usr/include/SDL']
running build
running build_ext
building 'surfutils' extension
creating build
creating build/temp.linux-i686-2.6
creating build/temp.linux-i686-2.6/src
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include -I/usr/include/python2.6 -I/usr/include/SDL -I/usr/include/python2.6 -c src/surfutils.c -o build/temp.linux-i686-2.6/src/surfutils.o
In file included from src/surfutils.c:1:
/usr/include/python2.6/pygame/pygame.h:68:17: error: SDL.h: Arquivo ou diretório inexistente
In file included from src/surfutils.c:1:
/usr/include/python2.6/pygame/pygame.h:312: error: expected specifier-qualifier-list before ‘SDL_VideoInfo’
/usr/include/python2.6/pygame/pygame.h:350: error: expected specifier-qualifier-list before ‘SDL_Surface’
src/surfutils.c:5: error: expected ‘)’ before ‘*’ token
src/surfutils.c: In function ‘PyCollisionPoint’:
src/surfutils.c:74: error: ‘SDL_Surface’ undeclared (first use in this function)
src/surfutils.c:74: error: (Each undeclared identifier is reported only once
src/surfutils.c:74: error: for each function it appears in.)
src/surfutils.c:74: error: ‘surf1’ undeclared (first use in this function)
src/surfutils.c:74: error: ‘surf2’ undeclared (first use in this function)
src/surfutils.c:74: warning: left-hand operand of comma expression has no effect
src/surfutils.c:92: error: ‘PySurfaceObject’ has no member named ‘surf’
src/surfutils.c:97: error: ‘SDL_SRCALPHA’ undeclared (first use in this function)
src/surfutils.c:111: error: ‘PySurfaceObject’ has no member named ‘surf’
src/surfutils.c:161: warning: implicit declaration of function ‘collisionPoint’
error: command 'gcc' failed with exit status 1

看起来它在尝试编译一个叫surfutils的扩展,这个扩展需要SDL的开发头文件。

所以我通过我的系统包管理器安装了libsdl1.2-dev这个包,结果一切正常。要在你的系统上编译这个扩展,你必须安装SDL的开发头文件。

所以你的问题其实是:我该如何在Windows上安装SDL的开发头文件,并让程序使用它们?

好吧,我可以回答第二个问题。你需要编辑setup.py文件:

#!/usr/bin/env python2.3

from distutils.core       import setup, Extension
from distutils.sysconfig  import get_config_vars

includes = []
includes.extend(get_config_vars('INCLUDEDIR'))
includes.extend(get_config_vars('INCLUDEPY'))
includes.append('/usr/include/SDL')

print 'DBG> include =', includes

setup(name='surfutils',
      version='1.0',
      ext_modules=[Extension(
                    'surfutils', 
                    ['src/surfutils.c'], 
                    include_dirs=includes,
                  )],
     )

修改第9行。它上面写着:

includes.append('/usr/include/SDL')

把这个路径改成你SDL头文件所在的位置,比如:

includes.append(r'C:\mydevelopmentheaders\SDL')

给游戏开发者留个备注,告诉他们你遇到了这个问题。这样他们可能会提供一个更好的方法来帮助你找到适合你平台的SDL头文件。

撰写回答