【问题标题】:getting ld to ignore undefined references when making a DLL在制作 DLL 时让 ld 忽略未定义的引用
【发布时间】:2013-07-01 17:02:58
【问题描述】:

我试图让gcc 的ld 在从一堆使用-fpic 标志编译的目标文件中组合一个共享库时忽略未解析的引用。

到目前为止,我尝试了很多选项,例如(为简洁起见,将许多目标文件的长文件名替换为一些小文件名):

ld --allow-shlib-undefined --unresolved-symbols=ignore-all -shared 1.o 2.o -o lib0.so
ld -G 1.o 2.o -o lib0.so

(我已将somewhere 标记为红色,-G 将允许未解析的引用,但没有运气。)

通过gcc(使用-Wl,--unresolved-symbols=ignore-all)运行它会减少未解析的引用,因为它默认链接为

-lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt

但它仍然抱怨没有-lopengl32 和-lgdi32。

在没有-fpic 的情况下编译库,将所有目标文件存储在.a 和ar 中并静态链接到它,同时将exe 程序链接到-lopengl32 和-lgdi32 导致工作完全正常。

实际的错误信息(为了简洁替换了长文件和函数名):

[file].o: In function `[function]': [file].cpp:19: undefined reference to `memcpy'
[file].o: In function `[function]': [file].cpp:26: undefined reference to `memcpy'

[file].o:[file].cpp:(.xdata+0x4c): undefined reference to `__gxx_personality_seh0'
[file].o:[file].cpp:(.xdata+0x74): undefined reference to `__gxx_personality_seh0'
[file].o:[file].cpp:(.rdata$[file]]+0x20):undefined reference to `__cxa_pure_virtual'
[file].o:[file].cpp:(.rdata$[file]]+0x28): undefined reference to `__cxa_pure_virtual'

我怎样才能让ld 忽略未定义的引用(至少来自libopengl32 和libgdi32,如果不是libstdc++ 等)并让要使用它的程序链接到它们?

【问题讨论】:

  • 当我用 g++ 而不是 gcc 编译的东西中出现 `__gxx_personality_seh0' 错误时,它就消失了。看看这是否适合你。
  • @JerryJeremiah 这只是存在的众多错误之一,基本上每次调用任何 std 函数(如 memcpy)、任何调用 opengl 等都会出错。关键是我尝试使用ld 而不是g++,程序正在编译.o 文件,我只想ld 从.o 文件中创建一个共享库,而不链接到其他库。使用ld 制作静态库时它工作得很好,但是当制作一个共享库时,它抱怨没有链接到 stdlib、opengllib 等。使用静态库的程序链接到 std、opengl 等,它应该理论上也适用于共享。

标签: gcc linker mingw ld


【解决方案1】:

其实ld 不是 gcc 的一部分。它是 binutils 的一部分。

尝试使用 gcc 链接,或者在 C++ 的情况下使用 g++ 而不是 ld。

此外,在 Windows 上,您无法使用未解析的符号构建共享库 (.dll) 或可执行文件 (.exe)。 “DLL 地狱”的一部分是您必须确保所有符号都能解析。

通常需要找到提供这些符号的库并添加所需的-l 标志。

但是像undefined reference to 'memcpy' 这样的错误表明它没有在标准库中找到东西。因此,要么您使用 MinGW 未附带的 ld,要么存在像 -nostdlib 这样的标志,告诉编译器不要查找标准库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多