how to use SDL in linux?

2019-05-31 02:26发布

问题:

The linux server at my school is just a bare-bone server, no x-windows, just a command line interface.

I tried to make a graphical c program on that server but found much difficulties. I use SDL library but every time I try to compile my code with gcc, I get:

testcursor.c:(.text+0x1ad): undefined reference to `SDL_Init'
testcursor.c:(.text+0x1b6): undefined reference to `SDL_GetError'
testcursor.c:(.text+0x200): undefined reference to `SDL_SetVideoMode'
...

Does anybody knows how to fix the problem? If not, is there anybody who has done graphic program in c in linux, please help! I would appreciate. Thanks.

回答1:

The recommended way of linking SDL on linux is using the sdl-config script.

example:

gcc -c test.c `sdl-config --cflags`
gcc -o test test.o `sdl-config --libs`
./test

or example:

gcc -o test test.c `sdl-config --cflags --libs`

where ` is the back tick character.



回答2:

Add -lSDL to your compile line.

This tells gcc to link your code to the SDL library.



回答3:

You're not linking to the SDL library. Your command should look something like this:

gcc testcursor.c -lsdl

That's assuming you're using the SDL that came with your Linux distro. If you downloaded it and built it by hand, you might need something more complicated, like this:

gcc -I/usr/local/include/sdl testcursor.c -L/usr/local/lib -lsdl

The -I and -L options tell gcc where to look for include files and libraries, respectively. The first command doesn't need them because if you use the stock SDL for your system, they're in the default locations.



标签: c graphics sdl