在Ubuntu系统上是自带SDL库的,之所以要源码编译安装SDL库主要有以下两个原因:1.可以使用指定的SDL版本;2.编译出可debug的版本。
一、获取源码、编译、安装SDL代码使用mercurial管理,先安装mercurial然后clone获取代码。
- $ sudo apt-get install aptitude autoconf
- $ sudo aptitude install mercurial
- $ hg clone -u SDL-1.2 http://hg.libsdl.org/SDL SDL-1.2
- $ cd SDL-1.2
$ ./autogen.sh
$ ./configure --prefix=/usr/
$ make
$ sudo make install
这样安装后,将出现/usr/lib/libSDL.so等文件。
二、一个简单的测试程序sdltest1.c源码:
- #include <SDL/SDL.h>
-
- int main( int argc, char* argv[])
- {
- // Fire up SDL, this starts all subsystems; audio video etc.
- SDL_Init(SDL_INIT_EVERYTHING);
- // Now Shut it down
- SDL_Quit();
-
- return 0;
- }
编译测试源代码:
- $ gcc -o test sdltest1.c -lSDL -g
产生test可执行程序,并且具有可以debug需要的symbol。
查看测试代码使用了哪些so库:
- $ ldd test | grep SDL
- libSDL-1.2.so.0 => /usr/lib/i386-linux-gnu/libSDL-1.2.so.0 (0xb76ea000)
从结果来看,测试文件并没有使用我们编译好的动态库(安装的路径在/usr/lib/libSDL.so)。
在gcc前添加LD_LIBRARY_PATH环境变量:
- $ export LD_LIBRARY_PATH=/usr/lib
再次编译:
- $ gcc -o test sdltest1.c -lSDL -g
再次查看测试代码使用了哪些so库:
- $ ldd test | grep SDL
- libSDL-1.2.so.0 => /usr/lib/libSDL-1.2.so.0 (0xb76ec000)
gdb调试该程序:
- $ gdb
- (gdb) file test
- Reading symbols from ./test...done.
- (gdb) b main
- Breakpoint 1 at 0x8048575: file sdltest1.c, line 6.
- (gdb) b SDL_Init
- Breakpoint 2 at 0x8048460
- (gdb) run
- Starting program: ./test
- [Thread debugging using libthread_db enabled]
- Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
- Breakpoint 1, main (argc=1, argv=0xbffff2b4) at sdltest1.c:6
- 6 SDL_Init(SDL_INIT_EVERYTHING);
- (gdb) s
- Breakpoint 2, SDL_Init (flags=65535) at ./src/SDL.c:151
- 151 {
- (gdb)
首先装载test,然后设置了两个断点,分别在main()函数和SDL_Init()函数。
三、复杂一点的SDL测试程序sdltest2.c源码:
- #include <stdio.h>
- #include <SDL/SDL.h>
-
- #define WIDTH 640
- #define HEIGHT 480
- #define BPP 4
- #define DEPTH 32
-
- void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
- {
- Uint32 *pixmem32;
- Uint32 colour;
-
- colour = SDL_MapRGB( screen->format, r, g, b );
-
- pixmem32 = (Uint32*) screen->pixels + y + x;
- *pixmem32 = colour;
- }
-
-
- void DrawScreen(SDL_Surface* screen, int h)
- {
- int x, y, ytimesw;
-
- if(SDL_MUSTLOCK(screen))
- {
- if(SDL_LockSurface(screen) < 0) return;
- }
-
- for(y = 0; y < screen->h; y++ )
- {
- ytimesw = y*screen->pitch/BPP;
- for( x = 0; x < screen->w; x++ )
- {
- setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
- }
- }
-
- if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
-
- SDL_Flip(screen);
- }
-
-
- int main(int argc, char* argv[])
- {
- SDL_Surface *screen;
- SDL_Event event;
-
- int keypress = 0;
- int h=0;
-
- if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
-
- if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
- {
- SDL_Quit();
- return 1;
- }
-
- while(!keypress)
- {
- DrawScreen(screen,h++);
- while(SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- keypress = 1;
- break;
- case SDL_KEYDOWN:
- keypress = 1;
- break;
- }
- }
- }
-
- SDL_Quit();
-
- return 0;
- }
效果如下:
四、动态库搜索路径的搜索先后顺序
-
编译目标代码时指定的动态库搜索路径;
-
环境变量LD_LIBRARY_PATH指定的动态库搜索路径;
-
配置文件/etc/ld.so.conf中指定的动态库搜索路径;
-
默认的动态库搜索路径/lib;
-
默认的动态库搜索路径/usr/lib。
阅读(6151) | 评论(0) | 转发(0) |