I've found a curiosity when compiling with clang (on a MacBook, if it helps). Suppose I have two files:
blah.c
int *p;
main.c
#include <stdio.h>
extern int *p;
int main() {
printf("%p\n", p);
return 0;
}
If I compile with
clang blah.c main.c
everything works out fine. However, if I do
clang -c blah.c
ar rcs libblah.a blah.o
clang main.c libblah.a
I get a linker error:
Undefined symbols for architecture x86_64:
"_p", referenced from:
_main in test-4bf0d6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Interestingly, if I initialize the variable in blah.c,
#include <stddef.h>
int *p = NULL;
the error goes away.
Also, compiling with gcc doesn't produce this behavior. What exactly is going on with clang here?
Here's the output from clang --version:
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: x86_64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin