#include <iostream>
using namespace std;
int x = 10;
int y = 20;
void func2();
int main()
{
func2();
return 0;
}
#include <iostream>
using namespace std;
int x, y;
void func2()
{
cout << "use extern1.txt var x = " << x << '\n';
cout << "use extern1.txt var y = " << y << '\n';
}
[beyes@centos C]$ vi extern2.cc
[beyes@centos C]$ g++ extern1.cc extern2.cc -o main
/tmp/ccClQsis.o:(.bss+0x0): multiple definition of `x'
/tmp/ccw6fzMj.o:(.data+0x0): first defined here
/tmp/ccClQsis.o:(.bss+0x4): multiple definition of `y'
/tmp/ccw6fzMj.o:(.data+0x4): first defined here
[beyes@centos C]$ g++ extern1.cc extern2.cc -o main
extern1.cc: In function ‘int main()’:
extern1.cc:10: 错误:‘func2’ 在此作用域中尚未声明
#include <iostream>
using namespace std;
int main()
{
extern int x, y;
cout << "using var defined after main: x = " << x << " y = " << y << '\n';
return 0;
}
int x = 10;
int y = 20;
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |