Pygame cannot find include file “sdl.h”

2019-07-04 03:28发布

问题:

I am trying to build a downloaded Python app on Windows that uses Pygame. I have installed Python 2.5 and Pygame 1.7.1. I am new to Python, but I just tried typing the name of the top level .py file on a Windows console command line. (I'm using Win XP Pro.)

This is the message that I get.

C:\Python25\include\pygame\pygame.h(68) : fatal error C1083: Cannot open include file: 'SDL.h': No such file or directory

I thought that Pygame was built on top of SDL and that a separate SDL install was not necessary. Nevertheless, I installed SDL 1.2.13 and added the SDL include folder to my %INCLUDE% environment variable. Still no luck.

I noticed that C:\Python25\Lib\site-packages\pygame includes several SDL*.DLL files, but there is no sdl.h header file anywhere in the python tree. Of course, I could copy the sdl headers into the C:\Python25\include\pygame folder, but that is a distasteful idea.

Anybody know the right way to set things up?

EDIT: The application is "The Penguin Machine" pygame app.

回答1:

I tried compiling and got the same errors on my linux box:

$ 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

Seems like it tries to compile a extension called surfutils which needs SDL development headers.

So I installed the libsdl1.2-dev package using my distribution package manager and it worked just fine. You must install SDL development headers in order to build it for your system.

So your question really is: How do I install SDL development headers on windows, and how I make the program use them?

Well, I can answer the second question. You must edit 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,
                  )],
     )

Change line 9. It says:

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

Change this path to wherever your SDL headers are, i.e.:

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

Leave a note to the game developer to say you're having this trouble. It could provide a better way of finding SDL headers on your platform.



回答2:

When you compile something the compiler looks up header files in several directories, some hardcoded and build in, and typically some given as arguments to the compiler (like for instance "gcc -I/usr/local/include ..."). One guess is that you are missing this. If not check out other possible causes to your error message.

You will need to have the SDL Development Libraries installed, but since you say "I could copy the sdl headers" it sounds like you already have. Then your problem is only to get the compiler to look in the include directory containing those files.