《基于嵌入式的家庭远程监控系统》项目总结(二)---SDL学习入门

1、搭建开发平台
(1)sudo apt-get install libsdl1.2-dev
最基本的开发包
(2)sudo apt-get install libsdl-image1.2-dev
关于图像的开发包
(3)sudo apt-get install libsdl-mixer1.2-dev
关于音频的开发包
(4)sudo apt-get install libsdl-ttf2.0-dev
关于文字的开发包
安装好以上四个开发包,开发平台算是搭建好了

2、最有代表性的简单程序,包括图像,文字,音乐,注释如下:
#include <stdio.h> #include <stdlib.h> #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <SDL/SDL_mixer.h> #include <SDL/SDL_ttf.h> static SDL_Surface* screen;//SDL窗口 int main(int argc, char *argv[]) { int quit = 0; SDL_Surface *text_sur;//文字容器 SDL_Surface *background;//图像容器 SDL_Event event; SDL_Color color; SDL_Rect srect, drect; Mix_Music *bgsound; TTF_Font *font; //初始化SDL if (SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO) < 0) { fprintf(stderr, "SDL init error:%s\n", SDL_GetError()); exit(0); } atexit(SDL_Quit);//注册退出时调用的操作 //设置SDL屏幕大小 screen = SDL_SetVideoMode(600, 400, 24, SDL_HWSURFACE); if (screen == NULL) { fprintf(stderr, "Set video mode failure:%s\n", SDL_GetError()); exit(0); } //设置SDL窗口标题 SDL_WM_SetCaption("test", NULL); /*显示图像*/ background = IMG_Load("background.jpg");//导入图像文件,并将图像放入文字容器 srect.x = 0; srect.y = 0; srect.w = background->w; srect.h = background->h; drect = srect;//设置截取范围 SDL_BlitSurface(background, &srect, screen, &drect);//将图像容器放入SDL窗口 /*显示文字*/ //初始化TTF if (TTF_Init() < 0) { fprintf(stderr, "TTF init error:%s\n", SDL_GetError()); return; } font = TTF_OpenFont("test.ttf", 40);//导入字体文件 color.r = 255; color.g = 0; color.b = 0;//设置文字颜色 text_sur=TTF_RenderText_Solid(font, "Hello, Welcome to GAME!!", color);//将文字放入文字容器 srect.x = 0; srect.y = 0; srect.w = text_sur->w; srect.h = text_sur->h; drect.x = (600 - text_sur->w) / 2; drect.y = (400 - text_sur->h) / 2; drect.w = text_sur->w; drect.h = text_sur->h;//设置截取范围 SDL_BlitSurface(text_sur, &srect, screen, &drect);//将文字容器放入SDL窗口 SDL_UpdateRect(screen, 0, 0, 600, 400);//更新SDL窗口,让新添加的容器显示 /*播放声音*/ Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);//打开音频 bgsound = Mix_LoadMUS("bgsound.mp3");//导入声音文件 Mix_PlayMusic(bgsound, -1);//播放音频 while (quit == 0) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: Mix_CloseAudio();//关闭音频 quit = 1; break; default: break; } } SDL_Delay(100); } return 0; }


问题:gcc main.c -o main编译时可能会出现如下错误:
main.c:(.text+0x19): undefined reference to `SDL_Init' main.c:(.text+0x22): undefined reference to `SDL_GetError' main.c:(.text+0x50): undefined reference to `SDL_Quit' main.c:(.text+0x79): undefined reference to `SDL_SetVideoMode' main.c:(.text+0x8c): undefined reference to `SDL_GetError' main.c:(.text+0xc7): undefined reference to `SDL_WM_SetCaption' main.c:(.text+0xd3): undefined reference to `IMG_Load' main.c:(.text+0x132): undefined reference to `SDL_UpperBlit' main.c:(.text+0x137): undefined reference to `TTF_Init' main.c:(.text+0x140): undefined reference to `SDL_GetError' main.c:(.text+0x174): undefined reference to `TTF_OpenFont' main.c:(.text+0x1a3): undefined reference to `TTF_RenderText_Solid' main.c:(.text+0x24c): undefined reference to `SDL_UpperBlit' main.c:(.text+0x279): undefined reference to `SDL_UpdateRect' main.c:(.text+0x29d): undefined reference to `Mix_OpenAudio' main.c:(.text+0x2a9): undefined reference to `Mix_LoadMUS' main.c:(.text+0x2c1): undefined reference to `Mix_PlayMusic' main.c:(.text+0x2d5): undefined reference to `Mix_CloseAudio' main.c:(.text+0x2ec): undefined reference to `SDL_PollEvent' main.c:(.text+0x2fc): undefined reference to `SDL_Delay' collect2: ld returned 1 exit status原因:那是因为该程序用到四个静态库,分别为:
(1)SDL
(2)SDL_image
(3)SDL_ttf
(4)SDL_mixer
需用-l参数连起来才能编译得过,如:gcc main.c -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer -o main
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值