//file name: test.h
//this is for include
int f(int ,int);
//file name: test.cpp
//.h and .cpp make .obj
int f(int a, int b) { return a+b; };
//file name: main.cpp
#include <iostream>
#include "test.h" //注意歐 是""歐
int main() {
cout << f(1,2) ;
return 0;
}
//在console mode底下你要做底下的事
//這些咚咚ide都會幫你做掉 所以你可以專心寫code
//不用管這些瑣事 但是這樣似乎會不懂為什麼程式要那樣寫...@@
g++ -c test.h test.cpp
//this will make test.obj
g++ -c test.h main.cpp -o main.obj
//this will make main.obj
g++ -o main.exe main.obj test.obj
//this make exe file
為什麼要這樣做?你想想看
如果iostream每次你compile的時候都要從頭到尾compile一次
那不是浪費時間嗎
所以就做成.obj 以加速compile 當然 那些obj不能被砍掉...
至於compile main的時候 只要有.h裡面的prototype就ok啦
如果你的project很大 .cpp .h 都有上萬行 你就會知道這樣對compile
時間的好處了
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 218.160.17.48