C 和 C++ 中的静态和外部全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11055802/
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-27 14:47:10  来源:igfitidea点击:

static and extern global variables in C and C++

c++cstaticglobal-variablesextern

提问by Cristi

I made 2 projects, the first one in C and the second one in C++, both work with same behavior.

我做了 2 个项目,第一个用 C 语言,第二个用 C++,都以相同的行为工作。

C project:

C项目:

header.h

头文件.h

int varGlobal=7;

main.c

主文件

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

void function(int i)
{
    static int a=0;
    a++;
    int t=i;
    i=varGlobal;
    varGlobal=t;
    printf("Call #%d:\ni=%d\nvarGlobal=%d\n\n",a,i,varGlobal,t);
}

int main() {
    function(4);
    function(6);
    function(12);
    return 0;
}

C++ project:

C++项目:

header.h

头文件.h

int varGlobal=7;

main.cpp

主程序

#include <iostream>
#include "header.h"
using namespace std;

void function(int i)
{
    static int a=0;
    int t=i;
    a++;
    i=varGlobal;
    varGlobal=t;
    cout<<"Call #"<<a<<":"<<endl<<"i="<<i<<endl<<"varGlobal="<<varGlobal<<endl<<endl; 
}

int main() {
    function(4);
    function(6);
    function(12);
    return 0;
}

I read that global variables are externby default and in C and staticby default in C++; so why does the C++ code works?

我读到全局变量在默认情况下是extern并且在 C 中是默认的,在 C++ 中默认是静态的;那么为什么 C++ 代码有效呢?

I mean int varGlobal=7;is same as static int varGlobal=7;and if it's static then it can be used only in the file it was declared, right?

我的意思是int varGlobal=7; static int varGlobal=7 相同;如果它是静态的,那么它只能在它声明的文件中使用,对吗?

回答by fbafelipe

Global variables are not externnor staticby default on C and C++. When you declare a variable as static, you are restricting it to the current source file. If you declare it as extern, you are saying that the variable exists, but are declared somewhere else, and if you don't have it declared elsewhere (without the externkeyword) you will get a link error (symbol not found).

全局变量在 C 和 C++ 上不是默认的,extern也不static是默认的。当您将变量声明为 时static,您将其限制为当前源文件。如果您将其声明为extern,则表示该变量存在,但在其他地方声明,如果您没有在其他地方声明它(没有extern关键字),您将收到链接错误(找不到符号)。

Your code will break when you have more source files including that header, on link time you will have multiple references to varGlobal. If you declare it as static, then it will work with multiple sources (I mean, it will compile and link), but each source will have its own varGlobal.

当您有更多包含该标题的源文件时,您的代码将中断,在链接时您将多次引用varGlobal. 如果您将其声明为static,那么它将与多个源一起使用(我的意思是,它将编译和链接),但每个源都有自己的varGlobal.

What you can do in C++, that you can't in C, is to declare the variable as conston the header, like this:

您在 C++ 中可以做而在 C 中不能做的是将变量声明为const标头,如下所示:

const int varGlobal = 7;

And include in multiple sources, without breaking things at link time. The idea is to replace the old C style #definefor constants.

并包含在多个来源中,而不会在链接时破坏内容。这个想法是替换旧的 C 风格#define的常量。

If you need a global variable visible on multiple sources and not const, declare it as externon the header, and declare it again, this time without the extern keywork, on a source file:

如果您需要一个在多个源上可见的全局变量而不是const,请将其声明为extern在头文件中,然后在源文件上再次声明它,这次没有 extern 键工作:

Header included by multiple files:

多个文件包含的标题:

extern int varGlobal;

In one of your source files:

在您的源文件之一中:

int varGlobal = 7;

回答by Mark Ransom

When you #includea header, it's exactly as if you put the code into the source file itself. In both cases the varGlobalvariable is defined in the source so it will work no matter how it's declared.

当您#include使用标头时,就好像您将代码放入源文件本身一样。在这两种情况下,varGlobal变量都是在源代码中定义的,因此无论如何声明它都可以工作。

Also as pointed out in the comments, C++ variables at file scope are not static in scope even though they will be assigned to static storage. If the variable were a class member for example, it would need to be accessible to other compilation units in the program by default and non-class members are no different.

同样如评论中所指出的,文件范围内的 C++ 变量在范围内不是静态的,即使它们将被分配给静态存储。例如,如果变量是类成员,则默认情况下程序中的其他编译单元需要可以访问它,非类成员也不例外。