c++clinkerstatic-librariesweak-linking

Prevent symbol from static to override libc symbol


I have a static library which I don't control that defines the symbol gettimeofday(). The main function needs to use this function but I don't want it to use the one from the library, I just want the regular function from the system to be used. How can I make the linker prefer the function from the system libraries?
I am on macOS using clang.
Things I tried:


Solution

  • Since you are building on macOS, use -lSystem before the static library, not -lc.

    This will result in a linker warning that it is “ignoring duplicate libraries: '-lSystem'”. If you wish to avoid that, you can use Clang’s -v switch to have it output the link command it uses, then reproduce that link command yourself with the later -lSystem omitted.