编译器可以查找SDL2类。如何正确包括并链接它们?
我刚开始冒险进入C ++。我下载此简单的直升机游戏我正在尝试要编译它,但我不知道如何正确包含并链接SDL2依赖项。
我的第一种方法是试图用GCC编译。我得到了以下命令:
gcc main.cpp ^
-I C:\code\SDL2\SDL2-2.0.22\include ^
-I C:\code\SDL2\SDL2_image-2.0.5\include ^
-I C:\code\SDL2\SDL2_mixer-2.0.4\include ^
-I C:\code\SDL2\SDL2_ttf-2.0.18\include ^
-L C:\code\SDL2\SDL2-2.0.22\lib\x64 ^
-L C:\code\SDL2\SDL2_image-2.0.5\lib\x64 ^
-L C:\code\SDL2\SDL2_mixer-2.0.4\lib\x64 ^
-L C:\code\SDL2\SDL2_ttf-2.0.18\lib\x64
但是编译器抱怨无法从SDL2 LIB中找到类和功能。例如,它给出的众多错误中的第一个是
In file included from heli.h:4:0,
from main.cpp:7:
loader.h: In function 'SDL_Surface* load_image(std::__cxx11::string, int)':
loader.h:27:57: error: 'SDL_DisplayFormat' was not declared in this scope
optimizedImage = SDL_DisplayFormat( loadedImage );
我然后尝试将代码库转换为Visual Studio项目,并在此教程,基本上具有以下配置:
包括目录:
c:\ code \ sdl2 \ sdl2-2.0.22 \ include; c:\ code \ sdl2 \ sdl2 \ sdl2_image-2.0.5 \ include; c:\ code \ code \ sdl2 \ sdl2 \ sdl2_mixer-2.0.0.0.4 \ increse \ code \ sdl2 \ sdl2_ttf-2.0.18 \ include; $(includepath)
库目录:
c:\ code \ sdl2 \ sdl2-2.0.22 \ lib \ x64; c:\ code \ sdl2 \ sdl2 \ sdl2_image-2.0.5 \ lib \ lib \ x64; c:\ code lib \ x64; c:\ code \ sdl2 \ sdl2_ttf-2.0.18 \ lib \ x64; $(librarypath)
linker/input/input/after input/after input/其他依赖关系:
sdl2.lib; sdl2main.lib; sdl2_image.lib; sdl2_mixer.lib; sdl2_ttf.lib;%(附加diDepentencies)
sdl2.lib; sdl2main.lib ; 或“'bar':未申报的标识符”。我错过了包括和链接C ++依赖性的内容?
I just started venturing into C++. I download this simple helicopter game and I'm trying to compile it, but I don't know how to properly include and link the SDL2 dependencies.
My first approach was trying to compile it with gcc. I got to the following command:
gcc main.cpp ^
-I C:\code\SDL2\SDL2-2.0.22\include ^
-I C:\code\SDL2\SDL2_image-2.0.5\include ^
-I C:\code\SDL2\SDL2_mixer-2.0.4\include ^
-I C:\code\SDL2\SDL2_ttf-2.0.18\include ^
-L C:\code\SDL2\SDL2-2.0.22\lib\x64 ^
-L C:\code\SDL2\SDL2_image-2.0.5\lib\x64 ^
-L C:\code\SDL2\SDL2_mixer-2.0.4\lib\x64 ^
-L C:\code\SDL2\SDL2_ttf-2.0.18\lib\x64
But the compiler complains about not being able to find classes and functions from the SDL2 libs. For instance, the first of the many errors it gives is
In file included from heli.h:4:0,
from main.cpp:7:
loader.h: In function 'SDL_Surface* load_image(std::__cxx11::string, int)':
loader.h:27:57: error: 'SDL_DisplayFormat' was not declared in this scope
optimizedImage = SDL_DisplayFormat( loadedImage );
I then tried to turn the code base into a Visual Studio project and configured it following this tutorial, basically having the following configuration:
Include Directories:
C:\code\SDL2\SDL2-2.0.22\include;C:\code\SDL2\SDL2_image-2.0.5\include;C:\code\SDL2\SDL2_mixer-2.0.4\include;C:\code\SDL2\SDL2_ttf-2.0.18\include;$(IncludePath)
Library Directories:
C:\code\SDL2\SDL2-2.0.22\lib\x64;C:\code\SDL2\SDL2_image-2.0.5\lib\x64;C:\code\SDL2\SDL2_mixer-2.0.4\lib\x64;C:\code\SDL2\SDL2_ttf-2.0.18\lib\x64;$(LibraryPath)
Linker/Input/Additional Dependencies:
SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;SDL2_ttf.lib;%(AdditionalDependencies)
But then I got the exact same missing dependencies, as "identifier 'foo' not found" or "'bar': undeclared identifier". What am I missing about including and linking C++ dependencies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
游戏的源代码是引用SDL而非SDL2。自版本1以来,函数名称或实现都有更改。实际上,如果您下载SDL和SDL2并在两个版本中查找SDL_VIDEO.H文件,您会看到SDL_DISPLAYFORMAT在sdl_video extres in版本1中的sdl_videformat中但不在版本2中。
中,对于包含过程,最好的方法是将其设置在您使用的IDE中。例如,在Visual Studio中,您可以通过在“解决方案属性 - 常规属性 - c/c/c ++ - >所有选项 - >附加内容”中添加目录来做到这一点。否则,我相信您需要知道正确的包容顺序或GCC会抱怨。
检查此讨论:
为什么订单链接哪些库有时会导致gcc中的错误?
The source code of the game is referencing the SDL not SDL2. There is a chance that the function names or implementations have changed since version 1. In fact, if you download SDL and SDL2 and look for the SDL_video.h files in both versions, you will see that SDL_DisplayFormat is in SDL_video header file of version 1 but not in version 2.
As for the include procedure, the best way to go about it is to set it up in the IDE that you are using. For example, in Visual Studio, you can do that by adding the directories in the "Solution Properties -> General Properties -> C/C++ -> All Options -> Additional Include Directories". Otherwise, I believe you need to know the right order of inclusion or gcc will complain.
Check this discussion:
Why does the order in which libraries are linked sometimes cause errors in GCC?