C语言 C中的共享全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3010647/
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
shared global variables in C
提问by Claudiu
How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at the top of all the other C files that want to use it? That sounds not ideal.
如何创建在 C 中共享的全局变量?如果我把它放在头文件中,那么链接器会抱怨变量已经定义。是在我的一个 C 文件中声明变量并手动将externs 放在想要使用它的所有其他 C 文件的顶部的唯一方法吗?这听起来并不理想。
采纳答案by DiGMi
In the header file write it with extern.
And at the global scope of one of the c files declare it without extern.
在头文件中用extern. 并且在其中一个 c 文件的全局范围内声明它没有extern.
回答by Hernán
In one header file (shared.h):
在一个头文件(shared.h)中:
extern int this_is_global;
In every file that you want to use this global symbol, include header containing the extern declaration:
在要使用此全局符号的每个文件中,包含包含 extern 声明的标头:
#include "shared.h"
To avoid multiple linker definitions, just one declaration of your global symbol must be present across your compilation units(e.g: shared.cpp) :
为了避免多个链接器定义,您的编译单元中必须只存在一个全局符号声明(例如:shared.cpp):
/* shared.cpp */
#include "shared.h"
int this_is_global;
回答by jim mcnamara
In the header file
在头文件中
header file
头文件
#ifndef SHAREFILE_INCLUDED
#define SHAREFILE_INCLUDED
#ifdef MAIN_FILE
int global;
#else
extern int global;
#endif
#endif
In the file with the file you want the global to live:
在包含您希望全局存在的文件的文件中:
#define MAIN_FILE
#include "share.h"
In the other files that need the extern version:
在需要 extern 版本的其他文件中:
#include "share.h"
回答by nos
You put the declaration in a header file, e.g.
您将声明放在头文件中,例如
extern int my_global;
In one of your .c files you define it at global scope.
在您的 .c 文件之一中,您在全局范围内定义它。
int my_global;
Every .c file that wants access to my_globalincludes the header file with the externin.
每个想要访问的 .c 文件都my_global包含带有externin的头文件。
回答by tritinia
If you're sharing code between C and C++, remember to add the following to the shared.hfile:
如果您在 C 和 C++ 之间共享代码,请记住将以下内容添加到shared.h文件中:
#ifdef __cplusplus
extern "C" {
#endif
extern int my_global;
/* other extern declarations ... */
#ifdef __cplusplus
}
#endif
回答by ivan
There is a cleaner way with just one header file so it is simpler to maintain. In the header with the global variables prefix each declaration with a keyword (I use common) then in just one source file include it like this
有一种只包含一个头文件的更简洁的方法,因此维护起来更简单。在带有全局变量的标头中,每个声明都带有一个关键字(我使用 common),然后在一个源文件中像这样包含它
#define common
#include "globals.h"
#undef common
and any other source files like this
以及任何其他像这样的源文件
#define common extern
#include "globals.h"
#undef common
Just make sure you don't initialise any of the variables in the globals.h file or the linker will still complain as an initialised variable is not treated as external even with the extern keyword. The global.h file looks similar to this
只要确保您没有初始化 globals.h 文件中的任何变量,否则链接器仍然会抱怨,因为即使使用 extern 关键字,初始化的变量也不会被视为外部变量。global.h 文件与此类似
#pragma once
common int globala;
common int globalb;
etc.
seems to work for any type of declaration. Don't use the common keyword on #define of course.
似乎适用于任何类型的声明。当然不要在#define 上使用 common 关键字。

