简体   繁体   English

C 中的外部声明和定义

[英]extern declaration and definition in C

a global variable may one to two different storage classes in C, to my best knowledge, and the declaration may be given with two different keywords, correspodingly据我所知,一个全局变量在 C 中可能有一到两个不同的存储类,并且声明可能用两个不同的关键字给出,相应地

extern int foo; //default
static int bar;

Static variables are only visible within the module of declaration, and cannot be exported.静态变量只在声明模块内可见,不能导出。 In case of extern declaration, the variable is in the common namespace of all modules linked, unless shadowed by static variable.在 extern 声明的情况下,变量位于所有链接模块的公共命名空间中,除非被静态变量遮蔽。

Whereas static variables have to be defined in their module, an extern variable may be defined somewhere else.静态变量必须在它们的模块中定义,而外部变量可以在其他地方定义。 It has to be defined if ever used.如果曾经使用过,则必须对其进行定义。

My compiler (GCC) accepts我的编译器 (GCC) 接受

static int bar = 5;

but casts a complain at但抱怨

extern int foo = 4;

It seems to be expected that extern variables are never defined with the keyword 'extern'.似乎预期 extern 变量永远不会用关键字“extern”定义。 This leads to the following question :这导致以下问题

What kind of storage class does the Object 'foo' in the example above have in the module where it is defined?上例中的Object 'foo' 在定义它的模块中具有什么样的存储类?

IIRC, extern is more of a hint to the compiler that it does not have to allocate storage for the value. IIRC, extern更多的是向编译器提示它不必为值分配存储空间。 The linker is expected to find the value in another compilation unit.链接器应在另一个编译单元中找到该值。 Usually extern is used in header files to indicate that the someone has defined storage associated with the name.通常在头文件中使用extern来指示某人已定义与名称相关联的存储。 The definition of the value does not include the extern keyword since the compiler has to allocate storage for the value in the compilation unit that includes the definition.值的定义不包括extern关键字,因为编译器必须在包含定义的编译单元中为值分配存储空间。

See extern storage class specifier for more details.有关更多详细信息,请参阅extern 存储类说明符

The extern variable will be defined with a global scope (exported) in the unit where it is defined: extern 变量将在定义它的单元中使用全局范围(导出)进行定义:

int baz = 5; int baz = 5;

默认存储类是auto

Actually you missed two storage classes: auto and register实际上你错过了两个存储类:auto 和 register
Register doesn't matter here but the default storage class is auto.注册在这里无关紧要,但默认的存储类是自动的。
Auto reserves space for a variable somewhere in the memory (which is usually what you want when you declare a variable).自动为内存中某处的变量保留空间(这通常是您在声明变量时想要的)。 It should be noted that for 'auto variables' new space will be allocated every time the scope of the variable is entered.应该注意的是,对于“自动变量”,每次进入变量范围时都会分配新的空间。 (ie calling the function func() from within func() when func() declares an 'auto' variable will result in two different variables and each call to func() will only know about its own variable. (即,当 func() 声明一个 'auto' 变量时,从 func() 中调用函数 func() 将导致两个不同的变量,并且每次调用 func() 只会知道它自己的变量。
From this follows that auto variables declared at the global scope will be unique (since the scope is enterd only once).因此,在全局范围内声明的 auto 变量将是唯一的(因为该范围仅被输入一次)。
Static variables however are always unique.然而,静态变量总是唯一的。 Unique in the sense that space will only be allocated once.独特之处在于空间只会被分配一次。 This is useful when func() calls func() and you want both function calls to operate on the same variable.当 func() 调用 func() 并且您希望两个函数调用都对同一个变量进行操作时,这很有用。
Extern variables are simply references to unique variables.外部变量只是对唯一变量的引用。
You use these when you want to access a global variable declared in a different file.当您想要访问在不同文件中声明的全局变量时,您可以使用这些。
Given the files 1.c and 2.c it does not suffice to declare "int global;"鉴于文件 1.c 和 2.c,声明“int global”是不够的; in both files because space would be allocated twice and the name clash would result in a linking error.在这两个文件中,因为空间将被分配两次并且名称冲突将导致链接错误。
Hence what you do is in one file to reserve space (using "int global;") and in the other file tell the linker to look for a variable of the name "global" in another file by writing "extern int global;".因此,您要做的是在一个文件中保留空间(使用“int global;”),并在另一个文件中告诉链接器通过编写“extern int global;”在另一个文件中查找名称为“global”的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM