内部链接和无链接的区别 [英] Difference between internal and no linkage

查看:61
本文介绍了内部链接和无链接的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考以下同一翻译单元中的代码:

Please refer to the following code that is in the same translation unit:

static int global_var; // file scope in C and global namespace scope in C++
                       // internal linkage
void f(void)
{
    static int local_var; // block scope in C and local scope in C++
                          // no linkage 
}

我的理解是:

My understanding is this:

  • 我可以在翻译单元的任何地方引用 global_var,因为它具有全局范围.
  • 我只能在函数 f 中引用 local_var,因为它具有局部作用域.
  • I can refer to global_var from anywhere in the translation unit because it has global scope.
  • I can refer to local_var only inside function f because it has local scope.

我的问题:

  1. 这两个变量在关联方面有何不同?
  2. 您能否举一个例子,说明内部无链接有什么不同,而且这种差异不仅来自范围?
  1. What is the difference beteen the two variables, in relation to linkage?
  2. Can you provide one example where internal and no linkage makes a difference, and the difference is derived not only from scope?

编辑

在 James Kanze 的回答和评论之后,我现在可以构建一个示例来显示内部和无链接属性之间的区别:

EDIT

After the answer and comments of James Kanze, I am now able to construct an example that shows the difference between the internal and no linkage attributes:

static int i; // definition
              // static storage
              // internal linkage

void f(void)
{
    extern int i; // declaration
                  // refers to the static i at file scope
                  // note that even though the specifier is extern
                  // its linkage is intern (this is legal in both C/C++)
    {
        int i; // definition
               // automatic storage
               // no linkage
    }
}


一些在解释所涉及的概念方面做得很好的文章:
- C 和C++
- 存储类说明符和存储持续时间
- C 和 C++ 中的链接

Some articles that do a good job at explaining the concepts involved:
- Scope regions in C and C++
- Storage class specifiers and storage duration
- Linkage in C and C++

推荐答案

第一:除了类型,变量还有其他三个特征:链接、范围和生命周期.所有四个属性在某种程度上是正交的,但以它们的方式相关联用语言表达,并以某种方式进行交互.

First: in addition to type, variables have three other characteristics: linkage, scope and lifetime. All four attributes are sort of orthogonal, but linked in the way they are expressed in the language, and do interact in some ways.

关于链接:链接确实会影响符号正在声明,而不是对象本身.如果没有链接,符号的所有声明都绑定到不同的对象,例如:

With regards to linkage: linkage really affects the symbol which is being declared, and not the object itself. If there is no linkage, all declarations of the symbol bind to different objects, e.g.:

int
func()
{
    int i;
    {
        int i;
    }
}

符号i没有链接,两个符号i是绑定的到两个不同的实体.一般来说,局部变量(在块范围内声明的变量)和函数参数有没有链接,无论类型和生命周期如何.

The symbol i has no linkage, and the two symbols i are bound to two different entities. Generally speaking, local variables (variables declared at block scope) and function arguments have no linkage, regardless of type and lifetime.

内部和外部链接相似,就是重复符号的声明绑定到同一实体:内部链接仅在翻译单元内绑定,外部交叉整个程序.所以给出:

Internal and external linkage are similar, in that repeated declarations of the symbol bind to the same entity: internal linkage binds only within the translation unit, external accross the entire program. So given:

static int i;   //  internal linkage...

在几个翻译单元中,i 绑定到一个单独的实体在每个翻译单元中.没有静态,你有外部链接,并且所有的 i 绑定到同一个实体.

in several translation units, the i binds to a separate entity in each translation unit. Without the static, you have external linkage, and all of the i bind to the same entity.

请注意,这仅适用于命名空间范围;所有实体非本地类的成员具有外部链接.

Note that this only holds at namespace scope; all entities which are members of a non-local class have external linkage.

这种类型有影响:const 的变量隐含地具有内部链接:

And that type has an impact: variables which are const implicitly have internal linkage:

int const i = 42;    //  same as static int const i...
extern int const j = 42;    //  external linkage.

最后,所有绑定到同一实体的声明必须声明它具有相同的类型.如果你违反了这条规则单个翻译单元(例如:

Finally, all declarations which bind to the same entity must declare it to have the same type. If you violate this rule in a single translation unit (e.g.:

extern int i;
//   ...
double i;

在同一个命名空间范围内),那么编译器应该抱怨.如果两个声明在不同的翻译单元中,然而,这是未定义的行为,谁知道会发生什么发生.(理论上,链接器可能会抱怨,但大多数不会.)

in the same namespace scope), then the compiler should complain. If the two declarations are in different translation units, however, it is undefined behavior, and who knows what will happen. (In theory, the linker could complain, but most don't.)

补充一点:联动是由第一个决定的可以引用实体的声明.所以如果我写:

One additional point: linkage is determined by the first declaration which can refer to the entity. So if I write:

static int i;

void
func()
{
    extern int i;
}

两个 i 指的是同一个实体,有内部链接.(为什么有人会写第二个声明是我无法理解的,但这是合法的.)

Both i refer to the same entity, which has internal linkage. (Why one would ever write the second declaration is beyond me, but it is legal.)

这篇关于内部链接和无链接的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆