Linking SDL in a C program

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-26 07:30:07

问题


I have recently become interested in using SDL after having learned some basics of C. I have installed SDL_image and SDL_mixer. They are located in /usr/local/include/SDL2. I realize that you must link against the header files however I am not sure how to do it. I am getting the error that SDL_mixer or SDL_image do not exist (depending on their line order in my source code). I have tried two different compilation commands and neither work here they are:

gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image

gcc filename.c -o test -I./usr/local/include/SDL2 -L./lib -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image

If anyone has any ideas I would appreciate it! Thanks in advance!


回答1:


you do not want that leading period

wrong

gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image

closer - not necessarily correct yet

gcc filename.c -o test  -L/usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image

any path with a leading period indicates to start from current dir and go relative instead of the intended absolute path

any system has the notion of a default library path which is fine if you are using a standard install ... so no need to do a

-I/include 

... sometime a library has helpers to identify and auto populate these ...

sdl and sdl2 do have such a helper ... this will give you those settings

gcc -o test filename.c  `pkg-config --cflags --libs sdl2`

notice those backticks ... another syntax style would be

gcc -o test filename.c  $(pkg-config --cflags --libs sdl2)

you are free to issue that stand alone just to take a peek

pkg-config --cflags --libs sdl2

... output

-D_REENTRANT -I/usr/include/SDL2 -lSDL2

Now onto your sdl mixer ... well it has a

pkg-config --cflags --libs SDL2_mixer

... output

-D_REENTRANT -I/usr/include/SDL2 -lSDL2_mixer -lSDL2

you probably do not want to mix sdl with sdl2 so replace mention of

-lSDL_mixer -lSDL_image

with

-lSDL2_mixer -lSDL2_image

as per

pkg-config --cflags --libs SDL2_image

... output

-D_REENTRANT -I/usr/include/SDL2 -lSDL2_image -lSDL2

so bundling these together

gcc -o test filename.c  -lSDL2main $(pkg-config --cflags --libs sdl2) $(pkg-config --cflags --libs SDL2_mixer)  $(pkg-config --cflags --libs SDL2_image)

or more simply combined to

gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs  sdl2 SDL2_mixer SDL2_image )

this can be stripped down to simply the following ... yet above syntax is more robust to changes

gcc -o test filename.c -D_REENTRANT -I/usr/include/SDL2  -lSDL2main -lSDL2 -lSDL2_mixer  -lSDL2_image 



回答2:


You can use sdl2-config to supply the appropriate flags to gcc:

gcc filename.c -o test `sdl2-config --cflags --libs`

sdl2-config --cflags produces a list of options that should be passed to the compiler, and sdl2-config --libs produces a list of libraries that should be linked to.



来源:https://stackoverflow.com/questions/42881682/linking-sdl-in-a-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!