Statically link libc while keeping executable dynamic?

I need this because my game uses SDL2 which needs dlopen, so completely static executable won’t work. Tried to do this with following:

target_link_options(game PRIVATE -static-libgcc -static-libstdc++)
target_link_libraries(game PRIVATE /lib/libc.a)

But CMake keeps to link libc dynamically. Using musl so static linking should work fine. What am I doing wrong?

Hello, mechakotik!

Try the -static flag:

target_link_options(game PRIVATE -static-libgcc -static-libstdc++ -static)

without target_link_libraries(game PRIVATE /lib/libc.a) (as it didn’t make effect as libc is linked implicitly).Preformatted text

This will make executable completely static, and dlopen won’t work. Or having working dlopen with static libc is overall impossible?

I also stuck with this, and found this repo: GitHub - pfalcon/foreign-dlopen: Small library allowing to use dlopen() from statically-linked applications (where statically-linked executable vs loaded shared library may use completely different libc's)
I hope that helps.
The big chore that project is based on Makefiles, I think that you should consume it via ExternalProject_Add() and create your own Find-module for this.

Note for myself: make PR with CMake build for the repo :blush:

P.S. for context, why glibc refuses to work with dlopen() , additional info would be find here: c++ - Why is statically linking glibc discouraged? - Stack Overflow