Reputation: 47
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?
Upvotes: 0
Views: 523
Reputation: 76
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?
Upvotes: 0