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