|
//test.c
int a=3;
//Main.c
#include <iostream>
using std::cout;
using std::endl;
extern int a;
main()
{
cout<<a<<endl;
}
用 g++ -o main main.c test.c 编译成功,运行输出 3,结果正确
但是,问题就在这里:若在 test.c 中把 int a=3 改为 const int a=3,
Main.c中用 extern const int a;
编译失败!
/tmp/ccRQXq7q.o: In function `main':Main.cpp .text+0x84):对‘a’未定义的引用
collect2: ld 返回 1
我是C++新手,书上没有提到 const 限制的 对象不能被其他文件的函数使用,故求救。 |
|