关键字Extern

Extern

参考

The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files.
C和c++中的 extern 关键字扩展了跨多个源文件的变量和函数的可见性。

In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.
在函数的情况下,隐式使用 extern 关键字(函数默认使用 extern)。但是对于变量,必须显式地使用关键字。

  1. extern with Functions

Example
The example has two C++ files named main.cpp and math.cppand a header file named math.h.

math.h:

int sum(int a, int b);

As you can see, the header file contains the declaration for a simple function called sum that takes two integers as parameters. The code for the math.cpp file is as follows:
可以看到,头文件包含一个简单函数 sum 的声明,该函数以两个整数作为参数。math.cpp 文件的代码如下:

int sum(int a, int b) {
    return a + b;
}

This file contains the definition for the previously declared sum function and it returns the sum of the given parameters as an integer.
这个文件包含前面声明的 sum 函数的定义,它以整数的形式返回给定参数的和。

Finally, the code for the main.cpp file is as follows:

#include <iostream>
#include "math.h"

int main () {
    std::cout << sum(10, 8) << std::endl;
}

This file includes the math.h header file containing the declaration for the sum function. Then inside the main function, the std::cout << sum(10, 8) << std::endl; statement calls the sum functions by passing 10 and 8 as the two parameters and prints out whatever the returned value is.
该文件包含头文件 math.h ,其中包含函数 sum 的声明。然后在主函数 main 中,
语句 std::cout << sum(10, 8) << std::endl; 通过传递10和8作为两个参数来调用 sum 函数,并输出返回值。

Now if you try to compile this program you’ll see it compiles without any problem and upon executing the resultant binary file, you’ll see following output in the console: 18
尝试编译这个程序,该程序成功地编译为二进制文件,执行后输出运行结果: 18

This works (even though the definition of the sum function is in a separate file than main.cpp) because all the functions in C/C++ are declared as extern. This means they can be invoked from any source file in the whole program. 这是可行的(即使定义 sum 函数的文件不是 main.cpp ),因为C/ c++中的所有函数都声明为 extern。这意味着可以从整个程序中的任何源文件调用它们。

You can declare the function as extern int sum(int a, int b) instead but this will only cause redundancy.
你可以将函数声明为 extern int sum(int a, int b),但这只会导致冗余(函数默认关键字 extern)。

  1. extern with Variables

Although the extern keyword is applied implicitly to all the functions in a C/C++ program, the variables behave a bit differently.
虽然 extern 关键字隐式应用于C/ c++程序中的所有函数,但变量的行为有一点不同。

Before I dive into the usage of extern with variables, I would like to clarify the difference between declaring a variable and defining it. 在深入研究 extern 与变量的用法之前,我想简述 声明变量定义变量 之间的区别。

Declaring a variable simply declares the existence of the variable to the program. It tells the compiler that a variable of a certain type exists somewhere in the code.
声明一个变量只是向程序声明该变量的存在。它告诉编译器代码中某个地方存在某种类型的变量。
You declare a float variable as follows:

float pi;

At this point, the variable doesn’t have any memory allocated to it. The compiler only knows that a float variable named pi exists somewhere in the code.
此时,变量没有分配任何内存。编译器只知道代码中存在一个名为 pi 的浮点变量。

Defining the variable, on the other hand, means declaring the existence of the variable, as well as allocating the necessary memory for it.
另一方面,定义变量意味着声明变量的存在,以及为它分配必要的内存
You define a variable as follows:

float pi = 3.1416;

You can declare a variable as many times as you want, but you can define a variable only once. This is because you can not allocate memory to the same variable multiple times.
可以根据需要多次声明一个变量,但只能定义一个变量一次。这是因为不能将内存多次分配给同一个变量。

Now, I’ll modify the math.h header file created in the previous section to contain the declaration for the pi variable as follows:
math.h

extern float pi;
int sum(int a, int b);

As you can see, the variable has been declared as an extern in the header file, which means this should be accessible anywhere in the program.
pi 变量在头文件中被声明为 extern,这意味着它应该在程序的任何地方都可以访问。

Next, I’ll update the main.cpp file as follows:

#include <iostream>
#include "math.h"

int main () {
    std::cout << pi << std::endl;
    std::cout << sum(10, 8) << std::endl;
}

I’ve added a new std::cout statement to print out the value of the pi variable. If you try to compile this program at this point, the compilation process will fail.
我添加了一个新的 std::cout 语句来输出变量 pi 的值。如果此时尝试编译此程序,则编译过程将失败。

This happens because, declaring the variable has let the compiler know that this variable exists somewhere in the program – but in reality it doesn’t. It has no memory allocation at all.
这是因为,变量声明已经让编译器知道这个变量存在于程序中的某个地方——但实际上它并不存在。因为它根本没有内存分配。

To get out of this problem, I’ll define the pi variable inside the math.cpp file as follows:
为了解决这个问题,将在 math.cpp 文件中定义 pi 变量如下:

float pi = 3.1416;

int sum(int a, int b) {
    return a + b;
}

The compilation process finishes without any issues, and if I execute the resultant binary, I’ll see the following output in my console:
编译过程完成后没有任何问题,运行生成的二进制文件,将在控制台看到以下输出:

3.1416
18

Since the pi variable has been declared as an extern and has been defined within the math.cpp file, the main.cpp file is able to access the value of pi without any problem at all.
因为 pi 变量被声明为 extern,并在 math.cpp 文件中定义,所以 main.cpp文件能够毫无问题地访问 pi 的值。

You can define the variable anywhere in the program but I chose the math.cpp file for definition to prove the point that this extern variable indeed is available to all the other source files as well.
您可以在程序的任何地方定义变量,但我选择 math.cpp 文件进行定义,以证明这个`extern 变量确实也可用于所有其他源文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值