什么是等效于 GCC ld 选项的 Microsoft Visual Studio --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.
相关文章