Search code examples
c++g++ldsdl-2

Cannot link SDL2 statically


I'm trying to link to SDL2 statically on Arch Linux in order to produce single, cross-platform executable for a game. SDL2 headers are installed in /usr/include/SDL2, and libraries in /usr/lib. Compiling dynamically with g++ -I/usr/include/SDL2 -lSDL2 hello.cpp works, as ./a.out produces a blank window, but I am unable to link SDL2 to the executable statically.

Contents of hello.cpp:

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

int main() {
    bool quit{false};
    SDL_Event inputEvent;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("Hello", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 1280, 0);

    while (!quit) {
        SDL_WaitEvent(&inputEvent);

        switch (inputEvent.type) {
        case SDL_QUIT:
            quit = true;
            break;
        }
    }

    SDL_Quit();

    std::cout << "Bye!";
    return 0;
}

g++ -I/usr/include/SDL2 -static -lSDL2 hello.cpp returns /usr/bin/ld: cannot find -lSDL2, while g++ -I/usr/include/SDL2 -l:libSDL2 hello.cpp returns /usr/bin/ld: cannot find -l:libSDL2.


Solution

  • It turns out that PKGBUILD of sdl2-hg in the AUR was renaming libSDL2.a to libSDL2main.a, hence the libSDL2.a not found error. Removing the problematic line from the PKGBUILD and recompiling SDL2 alleviated the issue.