【发布时间】:2014-04-22 04:20:34
【问题描述】:
为什么当我们在其他模块中重新定义变量时,可以将这些模块链接在一起?我编写了两个模块main.cpp 和module.cpp,如下所示:
//--main.cpp--//
#include <stdio.h>
int main(){
int i=5;
printf("The value of i is %d",i);
}
和
//--module.cpp--//
int i=7;
现在我正在编译并将它们与g++ 链接如下:
g++ -c main.cpp
g++ -c module.coo
g++ -o bin main.o module.o
没关系。但我预计编译只会成功,而链接器会抛出像redefining varaible 这样的异常。当我运行./bin 时,输出为The value of i is 5。
UPD:我知道如果我们将 i 声明为模块 main.cpp 的全局变量,链接器将抛出错误。
【问题讨论】:
标签: c++ variables module redefinition