编译SDL( solution:SDL_image.h:undefined reference to `IMG_Load' )

安装好SDL后,用gcc编译时,在后面要加配置选项(注意:配置选项的标号是TAB键上面的那个键)

比如:gcc -o sdl SDL.c `sdl-config --cflags --libs`


sdl_config作用

Now that we've created an SDL application, we need to compileit. SDL
applications are easy to compile; assuming a proper installation ofSDL, they
require just a few flags and libraries. The standard SDLdistribution includes a

program called sdl-config (similar to the gtk-config andglib-config scripts
that ship with the GTK+ toolkit) for supplying the appropriatecommand-line
arguments to gcc. The command sdl-config --cflags produces a listof the
options that should be passed to the compiler, and sdl-config--libs
produces a list of libraries that should be linked in. Theseoptions allow SDL
applications to compile correctly regardless of the location orversion of the
library. The following command will correctly build an SDLapplication:

$ gcc sdltest.c -o sdltest `sdl-config --cflags --libs`

我们设计了一个sdl的应用程序,我们需要编译它,sdl应用程序很容易编译,假设我们已经有一个安装恰当的sdl程序,编辑就只是需要一些flags还有库函数,标准的sdl安装包含一个叫做sdl_config的程序,它的作用是为命令行提供gcc所需要的参数,命令sdl-config--cflags 产生一些编译所需要的可选项。同时sdl-config --libs产生需要被连接的函数。这些允许sdl的应用程序正确的编译,不需要考虑版本问题。一下的这个命令就可以正确的编译一个文件


注意:有可能会出现undefined reference to `IMG_Load',这是由于使用了SDL_image.h的定义的函数

gcc error:
----------------------------------------------------------------------
LoadPNG.c:(.text+0x45): undefined reference to`IMG_Load'
collect2: ld returned 1 exit status
----------------------------------------------------------------------
egg pain for an hour....

before:
I compile like this:
>> gcc -Wall hello.c -o hello
if I do not use the extension sdl , everything is fine,
however,when I try to load png jpg tif ... pictures , I haveto use the extension sdl modules,the compile error appears likewhat I had shown in the beginning of this article.
I search Internet,I found the same problem:
http://www.elfnet.org/2011/11/30/sdl-undefined-reference-img_load-solved/
the solution is:
compile like following:
>>gcc -Wall hello.c -o hello  `sdl-config --cflags --libs` -lSDL_image
but you should not compilelike this:
>>gcc -Wall hello.c -o hello -lSDL -lSDL_image
the gcc donot know where isthe include file
you can try likefollowing
>>gcc -Wall hello.c -o hello -I /usr/include/SDL -lSDL -lSDL_image
then whatever extension you add ,just add -lSDL_XXXX ,theworld comes back again!


你可能感兴趣的:(sdl)