我想了解外部连杆和内部连杆以及它们的区别。
我也想知道的意思
除非声明为extern,否则Const变量默认为内部链接。
我想了解外部连杆和内部连杆以及它们的区别。
我也想知道的意思
除非声明为extern,否则Const变量默认为内部链接。
当前回答
在讨论这个问题之前,最好准确地了解翻译单元、程序和c++的一些基本概念(实际上链接也是其中之一)。您还必须知道什么是作用域。
我将强调一些重点,特别是之前回答中遗漏的部分。
链接是名称的属性,由声明引入。不同的名称可以表示同一个实体(通常是一个对象或一个函数)。因此,谈论实体的链接通常是毫无意义的,除非您确定实体将仅由某些特定声明(但通常是一个声明)中的唯一名称引用。
注意,对象是实体,而变量不是。在讨论变量的链接时,实际上关注的是所表示实体的名称(由特定声明引入)。名称的联动是在三种情况之一:不联动、内部联动或外部联动。
Different translation units can share the same declaration by header/source file (yes, it is the standard's wording) inclusion. So you may refer the same name in different translation units. If the name declared has external linkage, the identity of the entity referred by the name is also shared. If the name declared has internal linkage, the same name in different translation units denotes different entities, but you can refer the entity in different scopes of the same translation unit. If the name has no linkage, you simply cannot refer the entity from other scopes.
(哦…我发现我输入的只是在重复标准措辞…)
还有一些语言规范没有涵盖的令人困惑的地方。
Visibility (of a name). It is also a property of declared name, but with a meaning different to linkage. Visibility (of a side effect). This is not related to this topic. Visibility (of a symbol). This notion can be used by actual implementations. In such implementations, a symbol with specific visibility in object (binary) code is usually the target mapped from the entity definition whose names having the same specific linkage in the source (C++) code. However, it is usually not guaranteed one-to-one. For example, a symbol in a dynamic library image can be specified only shared in that image internally from source code (involved with some extensions, typically, __attribute__ or __declspec) or compiler options, and the image is not the whole program or the object file translated from a translation unit, thus no standard concept can describe it accurately. Since symbol is not a normative term in C++, it is only an implementation detail, even though the related extensions of dialects may have been widely adopted. Accessibility. In C++, this is usually about property of class members or base classes, which is again a different concept unrelated to the topic. Global. In C++, "global" refers something of global namespace or global namespace scope. The latter is roughly equivalent to file scope in the C language. Both in C and C++, the linkage has nothing to do with scope, although scope (like linkage) is also tightly concerned with an identifier (in C) or a name (in C++) introduced by some declaration.
The linkage rule of namespace scope const variable is something special (and particularly different to the const object declared in file scope in C language which also has the concept of linkage of identifiers). Since ODR is enforced by C++, it is important to keep no more than one definition of the same variable or function occurred in the whole program except for inline functions. If there is no such special rule of const, a simplest declaration of const variable with initializers (e.g. = xxx) in a header or a source file (often a "header file") included by multiple translation units (or included by one translation unit more than once, though rarely) in a program will violate ODR, which makes to use const variable as replacement of some object-like macros impossible.
其他回答
在c++
任何不在类或函数内嵌套的文件范围内的变量,在程序的所有翻译单元中都是可见的。这被称为外部链接,因为在链接时,该名称在翻译单元之外的任何地方都对链接器可见。
全局变量与普通函数具有外部联系。
文件作用域中的静态对象或函数名对于翻译单元是本地的。这是 称为内部联动
链接仅指在链接/加载时具有地址的元素;因此,类声明和局部变量没有关联。
当你编写一个实现文件(.cpp, .cxx等)时,你的编译器会生成一个翻译单元。这是你实现的源文件,加上你#包含在其中的所有头文件。
内部链接仅指翻译单元范围内的所有内容。
外部链接是指存在于特定翻译单元之外的事物。换句话说,可通过整个程序访问,这是所有翻译单元(或目标文件)的组合。
基本上
Extern链接变量在所有文件中可见 内部链接变量在单个文件中可见。
解释:默认情况下,const变量内部链接,除非声明为extern
缺省情况下,全局变量为外部链接 但是,const全局变量是内部链接 Extra, extern const全局变量是外部链接
这是一本关于c++中的链接的很好的材料
http://www.goldsborough.me/c/c++/linker/2016/03/30/19-34-25-internal_and_external_linkage_in_c++/
链接确定具有相同名称的标识符是否引用相同的对象、函数或其他实体,即使这些标识符出现在不同的转换单元中。标识符的链接取决于它是如何声明的。 有三种类型的连杆:
内部链接:标识符只能在翻译单元中看到。 外部链接:标识符可以在其他翻译单元中看到(并引用)。 没有链接:标识符只能在定义它们的作用域中看到。 链接不影响作用域
仅限c++:还可以在c++和非c++代码片段之间进行链接,这称为语言链接。
来源:IBM Program Linkage
在讨论这个问题之前,最好准确地了解翻译单元、程序和c++的一些基本概念(实际上链接也是其中之一)。您还必须知道什么是作用域。
我将强调一些重点,特别是之前回答中遗漏的部分。
链接是名称的属性,由声明引入。不同的名称可以表示同一个实体(通常是一个对象或一个函数)。因此,谈论实体的链接通常是毫无意义的,除非您确定实体将仅由某些特定声明(但通常是一个声明)中的唯一名称引用。
注意,对象是实体,而变量不是。在讨论变量的链接时,实际上关注的是所表示实体的名称(由特定声明引入)。名称的联动是在三种情况之一:不联动、内部联动或外部联动。
Different translation units can share the same declaration by header/source file (yes, it is the standard's wording) inclusion. So you may refer the same name in different translation units. If the name declared has external linkage, the identity of the entity referred by the name is also shared. If the name declared has internal linkage, the same name in different translation units denotes different entities, but you can refer the entity in different scopes of the same translation unit. If the name has no linkage, you simply cannot refer the entity from other scopes.
(哦…我发现我输入的只是在重复标准措辞…)
还有一些语言规范没有涵盖的令人困惑的地方。
Visibility (of a name). It is also a property of declared name, but with a meaning different to linkage. Visibility (of a side effect). This is not related to this topic. Visibility (of a symbol). This notion can be used by actual implementations. In such implementations, a symbol with specific visibility in object (binary) code is usually the target mapped from the entity definition whose names having the same specific linkage in the source (C++) code. However, it is usually not guaranteed one-to-one. For example, a symbol in a dynamic library image can be specified only shared in that image internally from source code (involved with some extensions, typically, __attribute__ or __declspec) or compiler options, and the image is not the whole program or the object file translated from a translation unit, thus no standard concept can describe it accurately. Since symbol is not a normative term in C++, it is only an implementation detail, even though the related extensions of dialects may have been widely adopted. Accessibility. In C++, this is usually about property of class members or base classes, which is again a different concept unrelated to the topic. Global. In C++, "global" refers something of global namespace or global namespace scope. The latter is roughly equivalent to file scope in the C language. Both in C and C++, the linkage has nothing to do with scope, although scope (like linkage) is also tightly concerned with an identifier (in C) or a name (in C++) introduced by some declaration.
The linkage rule of namespace scope const variable is something special (and particularly different to the const object declared in file scope in C language which also has the concept of linkage of identifiers). Since ODR is enforced by C++, it is important to keep no more than one definition of the same variable or function occurred in the whole program except for inline functions. If there is no such special rule of const, a simplest declaration of const variable with initializers (e.g. = xxx) in a header or a source file (often a "header file") included by multiple translation units (or included by one translation unit more than once, though rarely) in a program will violate ODR, which makes to use const variable as replacement of some object-like macros impossible.