提问人:Szymon Nowaczyk - Słomian 提问时间:10/13/2023 最后编辑:Remy LebeauSzymon Nowaczyk - Słomian 更新时间:10/13/2023 访问量:46
如果一个项目中有两个声明了相同变量的 .cpp 文件,为什么链接器在只有一个是静态的情况下不显示错误?
If there are two .cpp files in one project with the same variable declared, why linker shows no error if only one is static?
问:
假设我们有两个文件:和 ,它们都在一个项目中。在 中 我们创建全局变量,在 中创建 ..cpp
main.cpp
another.cpp
main.cpp
static int foo = 10
another.cpp
int foo = 20
现在,如果它们都不是,链接器将通知我们一个错误,指示声明了一个变量的多个实例。但在前面提到的例子中,Clion 实际上编译没有错误。static
在我的理解中,这两个变量在 中都是可见的,即使只有非变量在 中可见。为什么链接器不要求它们都成为?main.cpp
static
another.cpp
static
答: 暂无答案
评论
static
foo
foo
static
foo
foo
main.cpp
foo
another.cpp
foo
extern
main.cpp
main.cpp