I have a main program:
#include <stdio.h>
extern int a;
int main (int argc, char ** argv) {
int i;
printf ("Hello %p, %p\n", & i, & a);
return 0;
}
and a separate file, foo.c
, which contains my definition for the variable a
:
int a;
If I build like this:
clang -c main.c foo.c
clang main.o foo.o
./a.out
all is well. But, if I do the following:
ar rvs bar.a foo.o
I get a warning that I don't understand
r - foo.o
warning: /<elided>/ranlib:
warning for library: bar.a the table of contents is empty
(no object file members in the library define global symbols)
so I check my library, ensuring that my symbol is in there
nm bar.a
bar.a(foo.o):
0000000000000004 C _a
and then I do
clang main.o bar.a
I get the following error
Undefined symbols for architecture x86_64:
"_a", referenced from:
_main in main-5121f1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
what did I miss?