linkage
条评论linkage 的作用、或者说限制的是一个翻译单元 translation unit
中,编译器和链接器器如何使用符号。
linkage describes how names can or can not refer to the same entity throughout the whole program or one single translation unit.
-
Internal Linkage
name 只在当前编译单元的作用域中可见。When a name has internal linkage , the entity it denotes can be referred to by names from other scopes in the same translation unit.
-
External Linkage
name 在其他编译单元的作用域中、当前编译单元的作用域中可见。When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.
如果不特别指定:
- 对于
non-const
对象,默认是extern
- 对于
const
对象,默认是static
可以通过 extern
和 static
关键字,来显式控制符号的 linkage
:
1 | // in a global / namespace scope |
相比起用 static
,建议用匿名命名空间 anonymous namespaces
来控制 linkage,在其中还可以定义 class
等。
1 | namespace { |
可以看看标准,下次一定
https://eel.is/c++draft/basic.link
本文标题:linkage
文章作者:Henry Wu
发布时间:2024-02-08
最后更新:2025-08-20
原始链接:https://henrywu.netlify.app/2024/02/08/linkage/
版权声明:转载请注明出处。CC BY-NC-SA 4.0