我有一个主程序:
#include <stdio.h>
extern int a;
int main (int argc, char ** argv) {
int i;
printf ("Hello %p, %p\n", & i, & a);
return 0;
}
和一个单独的文件,foo.c
其中包含我对变量的定义a
:
int a;
如果我这样构建:
clang -c main.c foo.c
clang main.o foo.o
./a.out
一切都很好。但是,如果我执行以下操作:
ar rvs bar.a foo.o
我收到一个我不明白的警告
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)
所以我检查了我的图书馆,确保我的符号在那里
nm bar.a
bar.a(foo.o):
0000000000000004 C _a
然后我做
clang main.o bar.a
我收到以下错误
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)
我错过了什么?