Install SDL2 with Cpp on Ubuntu

Install SDL2 on Ubuntu

You can easy install from package or build self1.

sudo apt-get install libsdl2-dev

SDL2 example code with C++

The example code base on SDL2 document2, add C++ libary.

#include "SDL.h"
#include <iostream>

int main(int argc, char* argv[])
        SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;

    std::cout << "Hello SDL2 with C++" <<std::endl;


    return 0;

Build example code

g++ -I /usr/include/SDL2 main.cpp -o sdl-test -lSDL2 -lSDL2main

Note: See Where a Package is Installed on Ubuntu

Run the exmaple


If the process is correct, you will see that:

demo image


If you running it on ubuntu server, maybe show error:

Unable to initialize SDL: Failed to connect to the Mir Server

Because example have init SDL_INIT_VIDEO subsystem, you can remove it if you don’t need it.



Leave a Comment