What are practical applications of weak linking?(弱链接的实际应用是什么?)
问题描述
使用特殊的编译器命令可以声明一个符号weak.根据维基百科:
Using special compiler commands a symbol can be declared weak. According to Wikipedia:
弱符号是目标文件或动态库中可能被其他符号定义覆盖的符号定义
a weak symbol is a symbol definition in an object file or dynamic library that may be overridden by other symbol definitions
在哪些场景或哪些应用中需要弱符号?有哪些典型用例?
In what scenarios or for what applications do you need weak symbols? What are typical use cases?
推荐答案
弱链接的一个用途是在 C++ 标准中实现 可替换 函数.即:
One use of weak linking is implementing the replaceable functions in the C++ standard. Namely:
void *operator new(std::size_t);
void *operator new(std::size_t, std::nothrow_t const &) noexcept;
void *operator new[](std::size_t);
void *operator new[](std::size_t, const std::nothrow_t&) noexcept;
void operator delete(void *) noexcept;
void operator delete(void *, std::nothrow_t const &) noexcept;
void operator delete[](void *) noexcept;
void operator delete[](void *, std::nothrow_t const &) noexcept;
这些是实现必须提供的功能,但如果程序实现了它们,那么程序的实现将替换或覆盖实现的版本.这很容易通过弱链接实现.
These are functions which must be provided by the implementation, but if a program implements them then the program's implementation replaces or overrides the implementation's version. This is easily implemented via weak linkage.
这篇关于弱链接的实际应用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:弱链接的实际应用是什么?


基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01