Can I do things like static linking only SDL?

#include <SDL3/SDL.h>

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    return 0;
}
$ c++ -std=c++23 -ISDL/include main.cpp -LSDL/build -Wl,-Bstatic -lSDL3
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

I tried to change main.cpp to

#include <iostream>

int main()
{
    std::cout << "hello world\n";
}

and tried

$ c++ -std=c++23 main.cpp -Wl,-Bstatic 
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

I completely lost what is going on. How does c++ work?

Maybe you need a -Wl,-Bdynamic after the -lSDL3, in case other libraries are being handed to the linker after what is on your command line.

Alternately: link against libSDL3.a directly by specifying it on the command line, instead of using -lSDL3

The error occurs because you’re using -Wl,-Bstatic, which makes the linker try to statically link all libraries, including system ones like libgcc_s, which usually only exist as shared libraries on Linux. To fix this and statically link only SDL, modify your command to switch back to dynamic linking after SDL:

c++ -std=c++23 -ISDL/include main.cpp -LSDL/build -Wl,-Bstatic -lSDL3 -Wl,-Bdynamic

This links SDL statically while keeping the rest dynamic. Fully static builds require static versions of all libraries (like libc and libstdc++), which are uncommon on most Linux systems.

1 Like