C、C++编译,连接,extern连接

 1 //b.cpp
 2 
 3 #inlcude <iostream>
 4 
 5 void b()
 6 {
 7     std::cout<<"fun b";
 8 }
 9 
10 //a.cpp
11 extern void b();
12 
13 int main()
14 {
15     b();
16     return 0;
17 }
18 
19 //makefile
20 testA: a.o b.o
21     g++ -o testA a.o b.o
22 
23 clean:
24     rm testA a.o b.o

b.cpp 和 a.cpp之间没有任何联系,编译时能够分别编译经过生成a.o和b.o,连接时a.o中的b()没有定义,编译器自动的从b.o中查找到。ios

这里简单的体现了C语言的单独编译,相互连接的过程,即单独生成“.o”文件,共同生成执行文件。spa

相关文章
相关标签/搜索