Using SDL2 without CMake
Is it possible to use SDL2 in my C++ project without using CMake? How would I set that up?
Yes, it is possible to use SDL2 without CMake. Here's a general approach:
Step 1: Download the SDL2 development libraries (not just the source code) for your platform from the SDL website.
Step 2: Add the SDL2 include directory to your compiler's include path. For example, with g++:
g++ -I/path/to/SDL2/include ...Step 3: Link against the SDL2 libraries when compiling. For example:
g++ ... -L/path/to/SDL2/lib -lSDL2 -lSDL2_image -lSDL2_ttfStep 4: Make sure the SDL2 dynamic libraries are available at runtime, either by copying them to your executable's directory or adding their path to your system's library search path.
Here's an example of compiling a simple SDL2 program without CMake:
#include <SDL.h>
#include <iostream>
int main(int argc, char** argv) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init error: "
<< SDL_GetError() << "\n";
return 1;
}
std::cout << "SDL initialized successfully!";
SDL_Quit();
return 0;
}SDL initialized successfully!Compile with:
g++ -I/path/to/SDL2/include main.cpp -L/path/to/SDL2/lib -lSDL2 -o sdl_exampleWhile this approach works, using a build system like CMake can make it easier to manage dependencies and build your project on different platforms.
Building SDL2 from a Subdirectory (CMake)
A step-by-step guide on setting up SDL2 and useful extensions in a project that uses CMake as its build system