简体   繁体   English

为什么使用“ extern”创建变量是声明而不是定义?

[英]Why is creating a variable using 'extern' a declaration and not a definition?

I came across the following problem while reading ...just cant get the logic behind this. 我在阅读时遇到以下问题...只是无法理解其背后的逻辑。

auto int c;
static int c;
register int c;
extern int c;

It is given that the first three are definition and last one is declaration ..how come ? 给出的前三个是定义,最后一个是声明..怎么来的?

The last one with extern does not define storage for c . 最后一个带有extern没有定义c存储。 It merely indicates that c exists somewhere and the linker should be able to resolve it to some global c defined elsewhere. 它仅表示c存在于某个位置,并且链接程序应该能够将其解析为在其他位置定义的某些全局c

If you compiled and linked a single .c file and tried to use the last c you'd have a linker error. 如果编译并链接了一个.c文件,并尝试使用最后一个c ,则将出现链接器错误。 With the the first 3 c s you would not as they have substance (they've been defined ) in the current compilation unit. 对于前3个c您将不会在当前编译单元中拥有它们的实质(它们已定义 )。

If you'd like to learn more about extern and declaration vs definition here's a good article on the topic. 如果您想了解有关extern和声明与定义的更多信息,这是一篇有关该主题的好文章 To quote from that article: 引用该文章:

Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them 声明变量/函数仅声明变量/函数存在于程序中的某个位置,但未为其分配内存

The keyword extern references the fact that the definition of the variable (or possibly function) is elsewhere; 关键字extern引用了以下事实:变量(或可能的函数)的定义在其他位置; the compiler then links this declaration to a defined body in a separate file. 然后,编译器将此声明链接到单独文件中的已定义主体。 The prior three keywords state a declaration - the variable is not defined elsewhere and therefore are not prototypes . 前三个关键字声明一个声明-该变量未在其他位置定义,因此不是prototypes

For instance, say you have a project structure like so: 例如,假设您的项目结构如下:

..
-- main.c
-- client.c
-- client.h
-- server.c
-- server.h

When gcc compiles these using the header files, the header files typically will define the variables required for the program. gcc使用头文件编译这些文件时,头文件通常会define程序所需的变量。 This allocates a symbol that links to the declaration of the symbol in the .c files. 这将分配一个符号,该符号链接到.c文件中的符号declaration This is how compilers link up various project files with .o objects. 这就是编译器如何使用.o对象链接各种项目文件的方式。 You may be further interested in how this all appears by using objdump -d (assuming you're on Linux) to debug the actual disassembled structure of your program. 通过使用objdump -d (假设您在Linux上)调试程序的实际反汇编结构,您可能会对这一切的外观更感兴趣。

Enjoy and good luck! 祝您好运!

The first 3 statements actually allocate a place for the int . 前三个语句实际上为int分配了一个位置。

The last one does not. 最后一个没有。 All it does it tell the compiler is that somewhere in another compilation unit, an int called c will be defined. 它所做的一切告诉编译器是在另一个编译单元中的某个地方,将定义一个称为cint

If it is not defined, you'll get a linker error later on. 如果未定义,稍后会出现链接器错误。 Unsurprisingly enough, the linker will say that c is not defined. 不足为奇的是,链接器会说未定义c

The four sentences are declarations, however the first three sentences are also definitions. 四个句子是声明,但是前三个句子也是定义。

Read here about the difference between declaration and definition. 在这里阅读有关声明和定义之间的区别。

auto , static and register are modifiers for the variable. autostaticregister是该变量的修饰符。 Read de documentation about them. 阅读有关它们的文档。

extern is only declaration because you are telling the compiler that the definition of the variable or function is somewhere else - in another C module -. extern只是声明,因为您要告诉编译器变量或函数的定义在其他C模块中的其他位置。

Hope it helps! 希望能帮助到你!

First three are definition because it will set aside storage for the variables. 前三个是定义,因为它将为变量预留存储空间。

Last one will not allocated any storage for int c . 最后一个不会为int c分配任何存储空间。 It will just use storage allocated and named elsewhere. 它将仅使用在其他位置分配和命名的存储。

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

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