Building SDL2 from a Subdirectory (CMake)

Using SDL2 without CMake

Is it possible to use SDL2 in my C++ project without using CMake? How would I set that up?

Vector illustration representing computer programming

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_ttf

Step 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_example

While this approach works, using a build system like CMake can make it easier to manage dependencies and build your project on different platforms.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved