Load a library with LC_LOAD_DYLIB instead of LC_LOAD_WEAK_DYLIB

Hello,

I have create a dynamic library with gcc:

gcc -dynamiclib liblib1.c -o liblib1.dylib

I have create a binary which needs this dynamic library:

 gcc -L./ -llib1 test.c -o test

I have looked at mach-o commands in test binary. I have seen the library is loaded with LC_LOAD_WEAK_DYLIB. How can i load the library with LC_LOAD_DYLIB instead of LC_LOAD_WEAK_DYLIB ?

And i have a second question: Is it possible to compile the binary with a static linking of the library ? I have tried with -static option, like i do on Linux but it does not work on macOS and i don't understand why...

Thanks

Answered by DTS Engineer in 771558022

How can i load the library with LC_LOAD_DYLIB instead of LC_LOAD_WEAK_DYLIB?

Are you actually using GCC? Or invoking Clang via its gcc alias? What do you see if you run:

% gcc --version
Apple clang version 15.0.0 (clang-1500.0.40.1)
…

Is it possible to compile the binary with a static linking of the library?

Historically with Mach-O you either compiled your library as static or dynamic and, once you’d made that choice, your clients were stuck with it. In Xcode 15 we added the concept of a mergeable library. If your build your library as mergeable, clients can choose whether they want to link to it statically or dynamically. An Apple Library Primer has links to where you can learn more about this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

How can i load the library with LC_LOAD_DYLIB instead of LC_LOAD_WEAK_DYLIB?

Are you actually using GCC? Or invoking Clang via its gcc alias? What do you see if you run:

% gcc --version
Apple clang version 15.0.0 (clang-1500.0.40.1)
…

Is it possible to compile the binary with a static linking of the library?

Historically with Mach-O you either compiled your library as static or dynamic and, once you’d made that choice, your clients were stuck with it. In Xcode 15 we added the concept of a mergeable library. If your build your library as mergeable, clients can choose whether they want to link to it statically or dynamically. An Apple Library Primer has links to where you can learn more about this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Load a library with LC_LOAD_DYLIB instead of LC_LOAD_WEAK_DYLIB
 
 
Q