How do I specify to clang to statically link a certain library (in my case SDL2) and dynamically link the standard library?
Using -static
is not good, since the linker tries to statically link the standard library as well, which is prohibited in OSX. On the other hand, clang doesn't know -Wl,-Bstatic
.
When you want to “link a static library with dynamic library”, you really want to include the symbols defined in the static library as a part of the dynamic library, so that the run-time linker gets the symbols when it is loading the dynamic library.
You can't statically link a shared library (or dynamically link a static one). The flag -static will force the linker to use static libraries (. a) instead of shared (. so) ones.
Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.
Static libraries are created by copying all necessary library modules used in a program into the final executable image. The linker links static libraries as a last step in the compilation process. An executable is created by resolving external references, combining the library routines with program code.
Put your static libraries in, say, dir ./MyStaticLibs
and simply use -L./MyStaticLibs/ -l<StaticLibraryName>
.
Compiler prefers dynamic version over static version of library
If you have your dynamic library (random.dylib
) and static library (random.a
) in same directory then compiler will prefer and link with .dylib
not .a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With