我知道C中的全局变量有时有extern关键字。什么是extern变量?申报是什么样的?它的范围是什么?
这与跨源文件共享变量有关,但这究竟是如何工作的呢?我在哪里使用extern?
我知道C中的全局变量有时有extern关键字。什么是extern变量?申报是什么样的?它的范围是什么?
这与跨源文件共享变量有关,但这究竟是如何工作的呢?我在哪里使用extern?
当前回答
添加extern将变量定义转换为变量声明。关于声明和定义之间的区别,请参阅本线程。
其他回答
Extern告诉编译器信任你,这个变量的内存在其他地方声明了,所以它不会尝试分配/检查内存。
因此,您可以编译一个引用extern的文件,但是如果没有在某个地方声明该内存,则不能进行链接。
对于全局变量和库很有用,但是危险,因为链接器不进行类型检查。
In C a variable inside a file say example.c is given local scope. The compiler expects that the variable would have its definition inside the same file example.c and when it does not find the same , it would throw an error.A function on the other hand has by default global scope . Thus you do not have to explicitly mention to the compiler "look dude...you might find the definition of this function here". For a function including the file which contains its declaration is enough.(The file which you actually call a header file). For example consider the following 2 files : example.c
#include<stdio.h>
extern int a;
main(){
printf("The value of a is <%d>\n",a);
}
example1.c
int a = 5;
现在,当你一起编译这两个文件时,使用以下命令:
步骤1)cc -o ex example.c 步骤2)。/交货
输出结果如下:a的值<5>
Extern关键字与变量一起使用,以将其标识为全局变量。
它还表示可以使用使用extern声明的变量 关键字在任何文件中,尽管它在其他文件中声明/定义。
对于xc8,必须小心声明变量 在每个文件中都是相同的类型, 在一个文件中声明int型,在另一个文件中声明char型。 这可能会导致变量的破坏。
这个问题在大约15年前的一个微芯片论坛上得到了优雅的解决 /*参见“http:www.htsoft.com”/ /“论坛/ / showflat.php /猫/ 0 /数字/ 18766 / / /页面/ 0 # 18766”
但这种联系似乎不再起作用了……
所以我很快就会解释一下; 创建一个名为global.h的文件。
在其中声明以下内容
#ifdef MAIN_C
#define GLOBAL
/* #warning COMPILING MAIN.C */
#else
#define GLOBAL extern
#endif
GLOBAL unsigned char testing_mode; // example var used in several C files
现在在文件main.c中
#define MAIN_C 1
#include "global.h"
#undef MAIN_C
这意味着在main.c中变量将被声明为unsigned char。
现在在其他文件中简单地包括global.h将 将其声明为该文件的extern。
extern unsigned char testing_mode;
但是它将被正确地声明为unsigned char。
旧的论坛帖子可能解释得更清楚一点。 但在使用编译器时,这是一个真正的潜在问题 这允许您在一个文件中声明一个变量,然后在另一个文件中将其声明为不同的类型。这些问题与 也就是说,如果你在另一个文件中将testing_mode声明为int类型 它会认为它是一个16位的变量,并覆盖ram的其他部分,可能会破坏另一个变量。很难调试!
我使用了一个非常简短的解决方案,允许头文件包含对象的extern引用或实际实现。实际包含该对象的文件只是#define GLOBAL_FOO_IMPLEMENTATION。然后,当我向这个文件添加一个新对象时,它也会显示在那个文件中,而不需要我复制和粘贴定义。
我在多个文件中使用这种模式。因此,为了保持内容尽可能自包含,我只是在每个头文件中重用单个GLOBAL宏。我的标题是这样的:
//file foo_globals.h
#pragma once
#include "foo.h" //contains definition of foo
#ifdef GLOBAL
#undef GLOBAL
#endif
#ifdef GLOBAL_FOO_IMPLEMENTATION
#define GLOBAL
#else
#define GLOBAL extern
#endif
GLOBAL Foo foo1;
GLOBAL Foo foo2;
//file main.cpp
#define GLOBAL_FOO_IMPLEMENTATION
#include "foo_globals.h"
//file uses_extern_foo.cpp
#include "foo_globals.h