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[])
{
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
        SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

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

    SDL_Quit();

    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

./sdl-test

If the process is correct, you will see that:

demo image

Other

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.

Tags:

Updated:

Leave a Comment