Search code examples
c++macosclangstatic-linking

OSX: How to statically link a library and dynamically link the standard library?


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.


Solution

  • 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