Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 904
Arcanelab Avatar asked May 04 '16 21:05

Arcanelab


People also ask

Can you link a static library to a dynamic library?

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.

Can you statically link a shared 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.

What is the difference between using dynamically and statically linked libraries?

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.

How do I link a static library?

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.


1 Answers

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

like image 139
JamesWebbTelescopeAlien Avatar answered Nov 13 '22 23:11

JamesWebbTelescopeAlien