作者ja77 (LaTex)
看板EE_DSnP
標題Re: [問題] 有關於extern要怎麼用
時間Fri Oct 17 02:17:57 2008
For example:
In main.cpp:
int num;//num is a variable which is in
the global scope of the file, main.cpp.
void showNum();//the global function is extern by default.
int main(void)
{
showNum();
cout << "Input num:" << endl;
cin >> num;
showNum();
return 0;
}
In show.cpp
extern int num;//The compiler will inform the linker that unresolved references
//to variable num appear in the file, show.cpp due to the
//keyword, extern.
void showNum()
{
cout << "Num:" << num << endl;
return;
}
In the linux, you just type "g++ -o execFile main.cpp show.cpp."
※ 引述《bonoshi (sbo)》之銘言:
: 我想問一下
: Topic3講義第24張投影片的extern要怎麼用
: 我的意思是,我要怎麼告訴compiler這兩個檔案有關聯?
: 我一直無法compile成功直到我使用了後面教的include"xxx.cpp"
: (↑他一直出現[Linker error] undefined reference to `x' )(x是我要用的變數)
: 不過一旦include之後,我不用extern也能用了
: 那投影片上想說的,我該怎麼樣配置這些檔案呢?
: (一定是有什麼關鍵我沒有釐清)
--
應該是這樣吧!!!錯了我不負責XD
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.136.226.147
※ 編輯: ja77 來自: 220.136.226.147 (10/17 02:18)
推 bonoshi:謝謝回覆,我想問一下多檔案時,哪一個是主函式是不是由 10/17 19:17
→ bonoshi:main函式判定呢? 10/17 19:17
→ bonoshi:(↑應該說誰是主體) 10/17 19:17
推 ggegge:main函式只能有一個阿 (主函式是什麼意思?) 10/17 22:51
推 ric2k1:是的, 就是從 main 開始執行的, 而且只能有一個. 10/18 01:31
推 ric2k1:感謝接神, 這個例子簡單明瞭. 10/18 01:31
→ ric2k1:使用 DevC++ 的人, 請研究一下如何開專案 (project) 10/18 01:32
→ bonoshi:主函式其實就是main()的翻譯 10/18 10:01