⬆️ ⬇️

Installing sdl2 on major distributions

Hello, Habr. Recently I came across a new series of articles on sdl2, but without the installation procedure. It seems nothing complicated, but if it's interesting, then let's walk along the rake that can be expected. I will install for xcode10 on macOS 10.14.5, code :: blocks on Ubuntu 18.04 and Visual Studio 2019 Windows 10. Of course, you could use clion. One ide on all systems, but for some reason I don’t like their product. Nothing personal, just coding.



For verification, I will use the following code.



#include <iostream> #include <SDL2/SDL.h> const int WIDTH = 800, HEIGHT = 600; int main( int argc, char *argv[] ) { SDL_Init( SDL_INIT_EVERYTHING ); SDL_Window *window = SDL_CreateWindow( "Hello SDL World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI ); // Check that the window was successfully created if ( NULL == window ) { // In the case that the window could not be made... std::cout << "Could not create window: " << SDL_GetError( ) << std::endl; return 1; } SDL_Event windowEvent; while ( true ) { if ( SDL_PollEvent( &windowEvent ) ) { if ( SDL_QUIT == windowEvent.type ) { break; } } } SDL_DestroyWindow( window ); SDL_Quit( ); return EXIT_SUCCESS; } 


Ubuntu



We put code :: blocks from the store - no surprises here. Next, create a simple mian.cpp file for the test.



 #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; } 


If you are not going to compile from the terminal, then you need to specify the target in the project settings.





')

Fine. Next you need to install sdl2 itself. We write in the terminal



 sudo apt install libsdl2-dev -y 


After installation, let ide know to use sdl2. Go to the section “project” -> “build options”. We are interested in the "Linker settings" tab. In the “other linker options” window add "-lSDL2".







Replace with our code . Ok, window is drawn.







macOS



For installation, you can use the installation image from the Site . At the moment, the latest stable version is 2.0.9.



Download and copy to the / Library / Frameworks directory. Authentication may be required.



Run xcode and select the command line tool macOS project. Give the project a name and choose the C ++ language.



To add support for sdl2, go to “project settings” -> “build phases” -> “link binary with libraries” and add an element. In the window that opens, select "add other"







Add our file /Library/Frameworks/SDL2.framework and can check.



In case you prefer to use brew, remember that it installs only static libraries. In this case, you must pass the -I / usr / local / include flag.



Everything works, let's go further.







Windows



Install vs studio. Download the library . Unzip the contents and place where you like best. I very rarely use windows, so I don’t know where all the other libraries are stored.



Create a project with an empty cpp file, or a console application, as you like.

Go to “project” -> “properties” -> “C / C ++” -> “additional directories of include files”. Add the path to the unpacked directory, specifying include. In my case, \\ Mac \ Home \ Downloads \ SDL2-2.0.9 \ include, because I have a virtual machine.







After that, in the section “linker” -> “common” -> “additional catalogs of libraries” you need to specify the path to SDL2.dll, for example \\ Mac \ Home \ Downloads \ SDL2-2.0.9 \ lib \ x64.



It remains a bit, “linker” -> “input” -> “additional dependencies” we add what needs to be used, SDL2.lib, SDL2main.lib, SDL2test.lib. After that, you need to build a project. After collecting, copy the SDL2.dll file from the lib \ x64 or lib \ x32 directory and place it next to the executable file. Now try our code . Another point: in the “additional directories of included files” we specified the path to the plugin headers immediately, then



 #include <SDL2/SDL.h> 


replace with



 #include <SDL.h> 






Congratulations, we have successfully built applications for the main platforms. I hope you will not have difficulty installing.

Source: https://habr.com/ru/post/454396/



All Articles