static linking only some libraries
gcc -lsome_dynamic_lib code.c some_static_lib.a
You could also use ld
option -Bdynamic
gcc <objectfiles> -static -lstatic1 -lstatic2 -Wl,-Bdynamic -ldynamic1 -ldynamic2
All libraries after it (including system ones linked by gcc automatically) will be linked dynamically.
gcc objectfiles -o program -Wl,-Bstatic -ls1 -ls2 -Wl,-Bdynamic -ld1 -ld2
you can also use: -static-libgcc -static-libstdc++
flags for gcc libraries
keep in mind that if libs1.so
and libs1.a
both exists, the linker will pick libs1.so
if it's before -Wl,-Bstatic
or after -Wl,-Bdynamic
. Don't forget to pass -L/libs1-library-location/
before calling -ls1
.