gpt4 book ai didi

c++ - ld 的 -u 选项如何工作以及何时有用?

转载 作者:可可西里 更新时间:2023-11-01 15:23:01 26 4
gpt4 key购买 nike

我正在复制粘贴 section来自 ld 的人:-

-u symbol
--undefined=symbol
Force symbol to be entered in the output file as an undefined symbol. Doing this
may,for example, trigger linking of additional modules from standard libraries.
`-u' may be repeated with different option arguments to enter additional
undefined symbols.

如何实际使用这个选项?至于我如何在我的源代码中触发附加模块的链接,这个选项什么时候真正有用?

最佳答案

它对于从您的代码中未引用的静态库中提取目标文件很有用。与静态库链接时,链接器仅使用其中满足 undefined symbol 的对象。

此选项没有很多实际用例。通常没有必要链接一个未被引用的对象。据推测,如果它有用,它会在某处被引用。因此,将它包括在内肯定会有一些奇怪的副作用。

我能给你的唯一真实示例是在 Windows 下使用 Microsoft 链接器的类似选项。我想将 DirectX 错误消息库 (DXERR.LIB) 转换为 DLL,因此我使用了类似于以下的命令:

link /machine:ix86 /dll /out:dxerr.dll /base:0x400000
/include:_DXGetErrorStringA@4 /export:_DXGetErrorStringA@4
/include:_DXGetErrorStringW@4 /export:_DXGetErrorStringW@4
dxerr.lib mscvrt.lib user32.lib kernel32.lib

/include 开关相当于 ld 的 -u 选项。如果我不使用这些开关,我会得到一个空的 DLL,其中没有任何函数导出。

关于c++ - ld 的 -u 选项如何工作以及何时有用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834042/

26 4 0