本文介绍了在 C 程序中链接 SDL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习了 C 的一些基础知识后,我最近对使用 SDL 产生了兴趣.我安装了 SDL_image 和 SDL_mixer.它们位于/usr/local/include/SDL2 中.我意识到您必须链接头文件,但我不知道该怎么做.我收到 SDL_mixer 或 SDL_image 不存在的错误(取决于它们在我的源代码中的行顺序).我尝试了两种不同的编译命令,但都没有在这里工作:

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!

推荐答案

你不想要那个前导句点

you do not want that leading period

错误

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 和 sdl2 确实有这样的帮手……这将为您提供这些设置

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

...输出

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

现在到你的 sdl 混音器上......好吧它有一个

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

pkg-config --cflags --libs SDL2_mixer

...输出

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

您可能不想将 sdl 与 sdl2 混合使用,因此请替换提及

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

-lSDL_mixer -lSDL_image

-lSDL2_mixer -lSDL2_image

根据

pkg-config --cflags --libs SDL2_image

...输出

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

所以将它们捆绑在一起

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

或更简单地组合为

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

这篇关于在 C 程序中链接 SDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:56