我想了解外部连杆和内部连杆以及它们的区别。
我也想知道的意思
除非声明为extern,否则Const变量默认为内部链接。
我想了解外部连杆和内部连杆以及它们的区别。
我也想知道的意思
除非声明为extern,否则Const变量默认为内部链接。
当前回答
全局变量默认具有外部链接。通过在另一个文件中给出匹配的extern声明,可以将其作用域扩展到其他文件,而不是包含它。 通过在声明前加上关键字static,可以将全局变量的作用域限制在包含其声明的文件中。这样的变量被称为具有内部联系。
考虑下面的例子:
1. cpp
void f(int i);
extern const int max = 10;
int n = 0;
int main()
{
int a;
//...
f(a);
//...
f(a);
//...
}
函数f的签名将f声明为具有外部链接的函数(默认值)。它的定义必须在后面的文件或其他翻译单元中提供(如下所示)。 Max被定义为一个整数常数。常量的默认链接是内部的。使用关键字extern将其链接更改为外部链接。现在max可以在其他文件中访问。 N定义为整数变量。在函数体外定义的变量的默认链接是external。
2. cpp
#include <iostream>
using namespace std;
extern const int max;
extern int n;
static float z = 0.0;
void f(int i)
{
static int nCall = 0;
int a;
//...
nCall++;
n++;
//...
a = max * z;
//...
cout << "f() called " << nCall << " times." << endl;
}
max is declared to have external linkage. A matching definition for max (with external linkage) must appear in some file. (As in 1.cpp) n is declared to have external linkage. z is defined as a global variable with internal linkage. The definition of nCall specifies nCall to be a variable that retains its value across calls to function f(). Unlike local variables with the default auto storage class, nCall will be initialized only once at the first invocation of f(). The storage class specifier static affects the lifetime of the local variable and not its scope.
注意:关键字static起着双重作用。在全局变量定义中使用时,它指定内部链接。当在局部变量的定义中使用时,它指定变量的生存期将是程序的持续时间,而不是函数的持续时间。
其他回答
我认为c++中的内部和外部链接给出了一个清晰而简洁的解释:
A translation unit refers to an implementation (.c/.cpp) file and all header (.h/.hpp) files it includes. If an object or function inside such a translation unit has internal linkage, then that specific symbol is only visible to the linker within that translation unit. If an object or function has external linkage, the linker can also see it when processing other translation units. The static keyword, when used in the global namespace, forces a symbol to have internal linkage. The extern keyword results in a symbol having external linkage. The compiler defaults the linkage of symbols such that: Non-const global variables have external linkage by default Const global variables have internal linkage by default Functions have external linkage by default
链接确定具有相同名称的标识符是否引用相同的对象、函数或其他实体,即使这些标识符出现在不同的转换单元中。标识符的链接取决于它是如何声明的。 有三种类型的连杆:
内部链接:标识符只能在翻译单元中看到。 外部链接:标识符可以在其他翻译单元中看到(并引用)。 没有链接:标识符只能在定义它们的作用域中看到。 链接不影响作用域
仅限c++:还可以在c++和非c++代码片段之间进行链接,这称为语言链接。
来源:IBM Program Linkage
在c++
任何不在类或函数内嵌套的文件范围内的变量,在程序的所有翻译单元中都是可见的。这被称为外部链接,因为在链接时,该名称在翻译单元之外的任何地方都对链接器可见。
全局变量与普通函数具有外部联系。
文件作用域中的静态对象或函数名对于翻译单元是本地的。这是 称为内部联动
链接仅指在链接/加载时具有地址的元素;因此,类声明和局部变量没有关联。
正如dudewat所说,外部链接意味着符号(函数或全局变量)可以在整个程序中访问,内部链接意味着它只能在一个翻译单元中访问。
可以通过使用extern和static关键字显式地控制符号的链接。如果没有指定链接,那么对于非const符号,默认链接是extern(外部链接),对于const符号,默认链接是static(内部链接)。
// In namespace scope or global scope.
int i; // extern by default
const int ci; // static by default
extern const int eci; // explicitly extern
static int si; // explicitly static
// The same goes for functions (but there are no const functions).
int f(); // extern by default
static int sf(); // explicitly static
注意,与其使用静态(内部链接),不如使用匿名名称空间,您还可以在其中放入类。尽管它们允许外部链接,但匿名名称空间无法从其他翻译单元访问,这使得链接实际上是静态的。
namespace {
int i; // extern by default but unreachable from other translation units
class C; // extern by default but unreachable from other translation units
}
就“C”而言(因为静态关键字在“C”和“c++”之间具有不同的含义)
我们来谈谈C中的不同作用域
范围:它基本上是指我能看东西多长时间和多远。
Local variable : Scope is only inside a function. It resides in the STACK area of RAM. Which means that every time a function gets called all the variables that are the part of that function, including function arguments are freshly created and are destroyed once the control goes out of the function. (Because the stack is flushed every time function returns) Static variable: Scope of this is for a file. It is accessible every where in the file in which it is declared. It resides in the DATA segment of RAM. Since this can only be accessed inside a file and hence INTERNAL linkage. Any other files cannot see this variable. In fact STATIC keyword is the only way in which we can introduce some level of data or function hiding in 'C' Global variable: Scope of this is for an entire application. It is accessible form every where of the application. Global variables also resides in DATA segment Since it can be accessed every where in the application and hence EXTERNAL Linkage
默认情况下,所有函数都是全局的。以防万一,如果你需要 从外部隐藏一些函数在文件中,你可以前缀静态 函数的关键字。: -)