我正试图在C++中构建一个dll,其中我使用了一个C dll,它的原型是:int __stdcall foo();。
当链接时,编译器输出:
Warning: resolving _foo@0 by linking to _foo
Use --enable-stdcall-fixup to disable these warnings因此,我在链接时添加了该选项,命令如下:
g++ -std=c++0x -o fooLib.dll fooObj.o -lfooClib --enable-stdcall-fixup -shared
但似乎g++不知道这个选项:g++.exe: error: unrecognized option '--enable-stdcall-fixup'
当我只添加-enable-stdcall-fixup (一个连字符)时,它仍然会显示警告(看起来没有效果),输出也很奇怪:
g++ -std=c++0x -o fooLib.dll fooObj.o -lfooClib -enable-stdcall-fixup -shared
Warning: resolving _foo@0 by linking to _foo
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups
ld.exe: warning: cannot find entry symbol nable-stdcall-fixup; defaulting to 679c1000有谁知道我做错了什么吗?
g++ --version
g++ (GCC) 4.6.1发布于 2022-04-23 00:17:30
事实上,--enable-stdcall-fixup不是g++选项。它是一个链接器选项,您可以在ld(1)手册中找到它:
--enable-stdcall-fixup
--disable-stdcall-fixup
If the link finds a symbol that it cannot resolve, it will attempt
to do "fuzzy linking" by looking for another defined symbol that
differs only in the format of the symbol name (cdecl vs stdcall)
and will resolve that symbol by linking to the match. For example,
the undefined symbol "_foo" might be linked to the function
"_foo@12", or the undefined symbol "_bar@16" might be linked to the
function "_bar". When the linker does this, it prints a warning,
since it normally should have failed to link, but sometimes import
libraries generated from third-party dlls may need this feature to
be usable. If you specify --enable-stdcall-fixup, this feature is
fully enabled and warnings are not printed. If you specify
--disable-stdcall-fixup, this feature is disabled and such
mismatches are considered to be errors. [This option is specific
to the i386 PE targeted port of the linker]gcc能够识别一些常见的链接器选项,并将它们传递给ld。例如,gcc将用于在库代码中链接的-llibrary选项直接传递给链接器,以及下面相关的选项-e。无论何时发生这种情况,都会在gcc(1)手册中进行说明。
正如您已经发现的,--enable-stdcall-fixup不是这种情况,所以您需要显式传递它。为了将任意选项传递给链接器,gcc拥有-Wl。来自gcc(1)
-Wl,option
Pass option as an option to the linker. [...]所以在你的情况下,你会打电话给
g++ -Wl,--enable-stdcall-fixup [...]我没有在手册中提到的链接器的版本,所以对于我来说,它仍然是一个不被认可的选项。但是在您的系统上,考虑到链接器告诉您使用该选项,我只能假设它是识别它的版本。
顺便说一句,当你试着用一个破折号调用这个选项时,你遇到了一条红鲱鱼。您实际上调用了我前面提到的带有-e参数nable-stdcall-fixup的option gcc选项。来自gcc(1)
-e entry
--entry=entry
Specify that the program entry point is entry. The argument is
interpreted by the linker; the GNU linker accepts either a symbol
name or an address.因此,实际上您最终向链接器传递了一个选项,即当您执行程序时,您希望它从一个名为nable-stdcall-fixup的函数开始执行,而不是通常的main。
https://stackoverflow.com/questions/71869108
复制相似问题