0

I've read this post, and write the similar minimal example as below.

$ cat a.cpp
extern int a;
int main() {
  return a;
}

$ cat b.cpp
extern int b;
int a = b;

$ cat d.cpp
int b;

And using the same command to build it on ubuntu-16.04.

g++ a.cpp -L. -ld -lb

No surprise, an undefined reference linker error occur:

./libb.a(b.o): In function `__static_initialization_and_destruction_0(int, int)':
b.cpp:(.text+0x1b): undefined reference to `b'
collect2: error: ld returned 1 exit status

And i've checked the g++ version, it's 7.5.0.

However, when i use the same code and same compile command on my Mac(more precisely, os version Big Sur 11.4), no error occurs, it build and link it success.

I've learned that in OS X, g++ is actually clang, and here's the output of g++ --version in my laptop.

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: x86_64-apple-darwin20.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I've read this powerpoint about lld, it states that it can handle mutually-dependent lib properly so that user doesn't need to use mechanism like claim same lib in compile command many times or linker option like --start-group. But i am not sure whether it is MAC's clang default linker or not.

I've tried ld -v in my laptop, giving such output:

@(#)PROGRAM:ld  PROJECT:ld64-650.9
BUILD 00:19:30 Mar 17 2021
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 12.0.5, (clang-1205.0.22.9) (static support for 27, runtime is 27)
TAPI support using: Apple TAPI version 12.0.5 (tapi-1205.0.7.1)

After some google search, it seems to me that ld64 will also handle mutually-dependent static lib without user's involvement.

So my question is, which linker is OS X(BigSur) clang++/g++/cc command used by default? And did it really automatically handle static mutually-dependent static lib problem without user's involvement?

user8510613
  • 1,115
  • 5
  • 20

0 Answers0