What is the Microsoft Visual Studio equivalent to GCC ld option --whole-archive(什么是 Microsoft Visual Studio 等效于 GCC ld 选项 --whole-archive)
问题描述
将静态库链接到可执行文件时,通常会丢弃未引用的符号.在我的情况下,一些其他未使用的对象用于将它们各自的类注册到工厂中,如果这些对象被丢弃,则此注册失败.
When linking a static library against an executable, unreferenced symbols are normally discarded. In my case some otherwise unused objects are used to register their respective classes into a factory and if the objects are discarded, this registration fails.
在我们使用 gcc 的 Unix 下,我可以将标志 --whole-archive 传递给链接器 ld(参见下面 ld 文档的摘录),这使得 ld 不会丢弃任何对象.Visual C++ 有这样的东西吗?
Under Unix where we use gcc, I can pass the flag --whole-archive to the linker ld (see excerpt from ld documentation below), which makes ld not discard any objects. Is there anything like this for Visual C++?
--整个存档
对于命令行中提到的每个存档
`--whole-archive' 选项,包含存档中的每个目标文件
在链接中,而不是在存档中搜索所需的
目标文件.这通常用于将存档文件转换为
一个共享库,强制每个对象都包含在
生成的共享库.此选项可以多次使用.
For each archive mentioned on the command line after the
`--whole-archive' option, include every object file in the archive
in the link, rather than searching the archive for the required
object files. This is normally used to turn an archive file into
a shared library, forcing every object to be included in the
resulting shared library. This option may be used more than once.
推荐答案
据我所知,没有一个选项可以可靠地保证这一点.有一些优化选项的组合(默默地)禁用它,所以没有办法...... /INCLUDE
有效,但为此您需要提取和硬编码符号的错位名称.您有两种选择:(1) 确保所有注册商都包含(包含)在包含 main
的翻译单元中,并强制使用它们.(2) 放弃这个成语",使用显式注册.
To my knowledge, there is no single option which reliably guarantees that. There are combinations of optimizing options which (silently) deactivate this, so no way... /INCLUDE
works, but for that you need to extract and hardcode the mangled name of the symbol. You have two choices: (1) ensure, that all registrars are contained (included) in the translation unit containing main
and enforce their usage. (2) Give up this 'idiom' and use explicit registration.
注意:这个答案现在已经快 7 年了,关于 MSVC++ 工具链中选项可用性的声明已经过时了.尽管如此,我仍然建议不要依赖注册商模式并查看替代方案.由于此建议,请随意投反对票,但我认为投反对票有点不公平,因为同时该选项已添加到 Microsoft 链接器中.
Caution: this answer is now almost 7 years old and the statements regarding the availability of options in the MSVC++ toolchain are outdated. Nevertheless I still recommend not to rely on registrar pattern and look at the alternatives. Please feel free to down vote because of this recommendation but I guess it's a bit unfair to down vote because the option was added to Microsoft linker in the meantime.
这篇关于什么是 Microsoft Visual Studio 等效于 GCC ld 选项 --whole-archive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是 Microsoft Visual Studio 等效于 GCC ld 选项 --whole-archive
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01