在 Windows 上链接可执行文件的正确方法是什么?

2023-12-12

我需要在插件中使用主可执行文件中的一些符号。

链接可执行文件会导致以下链接器错误:

i686-w64-mingw32-g++ example.cpp -shared -I.. -std=c++11 -o test.dll ../../test.exe -static-libgcc -static-libstdc++ -fvisibility=hidden
[..]/test.exe:cygming-crtbegin.c:(.text+0x500): multiple definition of `__gcc_register_frame'
/usr/lib/gcc/i686-w64-mingw32/5.1.0/crtbegin.o:cygming-crtbegin.c:(.text+0x0): first defined here
[..]/test.exe:cygming-crtbegin.c:(.text+0x560): multiple definition of `__gcc_deregister_frame'
/usr/lib/gcc/i686-w64-mingw32/5.1.0/crtbegin.o:cygming-crtbegin.c:(.text+0x60): first defined here
[..]/test.exe: In function `ZlsRSoRK5Color':
[..]src/tools.h:212: multiple definition of `operator<<(std::ostream&, Color const&)'
/tmp/ccC97Hkz.o:example.cpp:(.text$_ZlsRSoRK5Color[__ZlsRSoRK5Color]+0x0): first defined here
../../test.exe: In function `ZN7MessageILb0EElsIcEERS0_OT_':
[..]/src/tools.h:241: multiple definition of `Message<false>& Message<false>::operator<< <char>(char&&)'
/tmp/ccC97Hkz.o:example.cpp:(.text$_ZN7MessageILb0EElsIcEERS0_OT_[__ZN7MessageILb0EElsIcEERS0_OT_]+0x0): first defined here
[..]/test.exe:crtexe.c:(.idata+0x3f0): multiple definition of `_imp__GeoIP_country_code'
[..]/test.exe:crtexe.c:(.idata+0x3f0): first defined here
[..]/test.exe:crtexe.c:(.idata+0x3f4): multiple definition of `_imp__GeoIP_country_name'
[..]/test.exe:crtexe.c:(.idata+0x3f4): first defined here
/usr/lib/gcc/i686-w64-mingw32/5.1.0/crtbegin.o:cygming-crtbegin.c:(.text+0x22): undefined reference to `_Jv_RegisterClasses'
collect2: error: ld returned 1 exit status

现在,如果我使用以下命令构建主要可执行文件-shared -Wl,--export-all-symbols然后链接到test.exe作品, 但是 Windows 加载程序(或者至少是 wine 加载程序)抱怨test.exe是一个 dll。

所以我需要重新链接test.exe再次没有-shared所以我可以跑test.exe.

i.e.:

# produce the import executable
i686-w64-mingw32-g++ tools.o main.o [...] -o ../test.exe -shared -Wl,--export-all-symbols [...] -static-libgcc -static-libstdc++

# produce the real executable
i686-w64-mingw32-g++ tools.o main.o [...] -o ../test.exe -Wl,--export-all-symbols [...] -static-libgcc -static-libstdc++

这太黑客了,但最后我确实有了一个可以工作的插件......

回答我的问题:

有没有更好的方法来实现这一点(不传递函数指针)?

I know MSVC能够输出可执行文件的导入库,是否有类似的方法MinGW?

我尝试添加-Wl,--out-implib,test.a链接器标志以获取可执行文件的导入库, 但--out-implib链接可执行文件时似乎被忽略。


在这种情况下,您可能do想要限定回调符号,在.exe,与__declspec(dllexport)属性。在我的 Linux Mint Debian 机器上进行交叉编译,以下最小示例适合我:

$ cat foo.c
#include <stdio.h>

int __declspec(dllexport) foo( int bar ){ return bar << 2; }
int main(){ printf( "%d\n", foo( 4 ) ); return 0; }

$ mingw32-gcc -o ~/src/exp/foo.exe -Wl,--out-implib=libfoo.dll.a foo.c

这会产生both a working可执行文件,and一个导入库来映射其导出的符号,以便在链接插件时使用,只需one在前面的命令中调用链接器(如在 wine 下运行可执行文件时所见,并使用本机 linux 列出导入库nm tool):

$ ~/src/exp/foo.exe
16

$ nm -A libfoo.dll.a
libfoo.dll.a:d000002.o:00000000 I _foo_exe_iname
libfoo.dll.a:d000002.o:00000000 i .idata$4
libfoo.dll.a:d000002.o:00000000 i .idata$5
libfoo.dll.a:d000002.o:00000000 i .idata$7
libfoo.dll.a:d000000.o:         U _foo_exe_iname
libfoo.dll.a:d000000.o:00000000 I __head_foo_exe
libfoo.dll.a:d000000.o:00000000 i .idata$2
libfoo.dll.a:d000000.o:00000000 i .idata$4
libfoo.dll.a:d000000.o:00000000 i .idata$5
libfoo.dll.a:d000001.o:00000001 a @feat.00
libfoo.dll.a:d000001.o:00000000 T _foo
libfoo.dll.a:d000001.o:         U __head_foo_exe
libfoo.dll.a:d000001.o:00000000 i .idata$4
libfoo.dll.a:d000001.o:00000000 i .idata$5
libfoo.dll.a:d000001.o:00000000 i .idata$6
libfoo.dll.a:d000001.o:00000000 i .idata$7
libfoo.dll.a:d000001.o:00000000 I __imp__foo
libfoo.dll.a:d000001.o:00000000 t .text

同样,可执行文件在 WinXP 中运行得很好(在 LMDE 机器上的 VirtualBox 中运行,〜/src/exp 映射为 WinXP VM 中的驱动器 E:,并从 MSYS shell 调用):

$ /e/foo.exe
16

FWIW,当添加以下内容时,我可以重现您创建可运行可执行文件的失败-shared链接器调用的属性;正如您所注意到的,这是为了创建 DLL(与可执行文件的格式不同)only在标头中嵌入不同的幻数;否则它们基本上是相同的)。

总之:

  • 不指定-shared链接可执行文件时。

  • 是否限定要从可执行文件导出的符号__declspec(dllexport)属性。

  • 请指定-Wl,--out-implib=lib<exename>.dll.a属性,当 链接可执行文件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Windows 上链接可执行文件的正确方法是什么? 的相关文章

随机推荐