作者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)
1F:推 bonoshi:谢谢回覆,我想问一下多档案时,哪一个是主函式是不是由 10/17 19:17
2F:→ bonoshi:main函式判定呢? 10/17 19:17
3F:→ bonoshi:(↑应该说谁是主体) 10/17 19:17
4F:推 ggegge:main函式只能有一个阿 (主函式是什麽意思?) 10/17 22:51
5F:推 ric2k1:是的, 就是从 main 开始执行的, 而且只能有一个. 10/18 01:31
6F:推 ric2k1:感谢接神, 这个例子简单明了. 10/18 01:31
7F:→ ric2k1:使用 DevC++ 的人, 请研究一下如何开专案 (project) 10/18 01:32
8F:→ bonoshi:主函式其实就是main()的翻译 10/18 10:01