SDL start
SDL是编写跨平台游戏和多媒体应用的支持库,包含了对图形、声音、游戏杆、线程等支持,内容丰富、应用广泛。
ubuntu15.04上安装SDL:
Source Code在 http://www.libsdl.org/download-1.2.php
上,下载SDL-1.2.15.tar.gz。解压tar -zxvf SDL-1.2.15.tar.gz
,在加压后的文件夹SDL-1.2.15中使用root账户进行安装三部曲./configure; make; make install
安装完成后根据安装过程中出现log我们可以知道Libraries在/usr/local/lib
,而编码所需要的文件SDL.h
在/usr/local/include/SDL中,这依靠find命令可以查找到。在INSTALL文件中可以发现更多的东西:
Look at the example programs in ./test, and check out the HTML
documentation in ./docs to see how to use the SDL library.
我们可以将/usr/local/lib中的库文件放到/usr/lib中,将/usr/local/include/SDL中的头文件放到/usr/include中,然后gcc就能找到了。
对于写好的程序,如果直接编译gcc -o sdl1 sdl1.c
,那会出现错误:
/tmp/ccEE9bhU.o: In function `main':
sdl1.c:(.text+0x17): undefined reference to `SDL_Init'
sdl1.c:(.text+0x23): undefined reference to `SDL_GetError'
sdl1.c:(.text+0x59): undefined reference to `SDL_SetVideoMode'
sdl1.c:(.text+0x6a): undefined reference to `SDL_GetError'
sdl1.c:(.text+0x95): undefined reference to `SDL_Quit'
sdl1.c:(.text+0xc3): undefined reference to `SDL_MapRGB'
sdl1.c:(.text+0xd9): undefined reference to `SDL_FillRect'
sdl1.c:(.text+0xef): undefined reference to `SDL_UpdateRect'
sdl1.c:(.text+0x10c): undefined reference to `SDL_Delay'
collect2: error: ld returned 1 exit status
在命令的最后加上连接选项-lSDL
,如:gcc -o sdl1 sdl1.c -lSDL
例子:创建一个红色屏幕,并停留5秒。
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
int main(){
SDL_Surface *screen;
unsigned int