C/C++ 全局 vs 静态全局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7837190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:34:22  来源:igfitidea点击:

C/C++ global vs static global

c++cstaticglobal-variables

提问by Vladp

Possible Duplicate:
Static vs global

可能的重复:
静态与全局

I'm confused about the differences between global and static global variables. If static means that this variable is global only for the same file then why in two different files same name cause a name collisions?

我对全局变量和静态全局变量之间的差异感到困惑。如果静态意味着此变量仅对同一个文件是全局的,那么为什么在两个不同的文件中相同的名称会导致名称冲突?

Can someone explain this?

有人可以解释一下吗?

回答by Shahbaz

Global variables (not static) are there when you create the .ofile available to the linker for use in other files. Therefore, if you have two files like this, you get name collision on a:

static当您创建.o可供链接器在其他文件中使用的文件时,全局变量(不是)就在那里。因此,如果您有两个这样的文件,您会遇到名称冲突a

a.c:

交流:

#include <stdio.h>

int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

b.c:

公元前:

int a;

int compute(void)
{
    a = 0;
    return a;
}

because the linker doesn't know which of the global as to use.

因为链接器不知道a要使用哪个全局s。

However, when you define static globals, you are telling the compiler to keep the variable only for that file and don't let the linker know about it. So if you add static(in the definition of a) to the two sample codes I wrote, you won't get name collisions simply because the linker doesn't even know there is an ain either of the files:

但是,当您定义静态全局变量时,您是在告诉编译器只为该文件保留变量,并且不要让链接器知道它。因此,如果您将static(在 的定义中a)添加到我编写的两个示例代码中,您将不会仅仅因为链接器甚至不知道其中a任何一个文件中存在名称冲突:

a.c:

交流:

#include <stdio.h>

static int a;

int compute(void);

int main()
{
    a = 1;
    printf("%d %d\n", a, compute());
    return 0;
}

b.c:

公元前:

static int a;

int compute(void)
{
    a = 0;
    return a;
}

This means that each file works with its own awithout knowing about the other ones.

这意味着每个文件都使用自己的文件a而不知道其他文件。



As a side note, it's ok to have one of them staticand the other not as long as they are in different files. If two declarations are in the same file (read translation unit), one staticand one extern, see this answer.

作为旁注,static只要它们在不同的文件中,就可以拥有其中一个和另一个。如果两个声明在同一个文件中(读取翻译单元),一static和一extern,请参阅此答案

回答by Jerry Coffin

A name that's static in each file should notcause name collisions. If you're seeing that, please post (short) demo code showing it, along with the exact compiler you're using so we can properly verify the code and assuming it's correct, proper vilify the compiler.

这是静态的每个文件的名称应该不会引起名称冲突。如果你看到了,请发布展示它的(简短)演示代码,以及你正在使用的确切编译器,以便我们可以正确验证代码并假设它是正确的,正确地诋毁编译器。

Just FWIW, the preferred method in C++ is to use an anonymous namespace instead:

只是 FWIW,C++ 中的首选方法是使用匿名命名空间:

namespace { 
    int not_a_static_variable;
}

In all honesty, no I can't point to a lot of objective advantage to that though...

老实说,不,虽然我不能指出很多客观优势......