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.

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% 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