使用 --enable-stdcall-fixup 禁用这些警告

Use --enable-stdcall-fixup to disable these warnings

我正在尝试用 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

确实,--enable-stdcall-fixup 不是 g++ 选项。这是一个 linker 选项,您可以在 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 能够识别一些常见的 linker 选项并将它们传递给 ld。例如,gcc 将库代码中用于 link 的 -llibrary 选项直接传递给 linker,以及一个相关的选项 -e以下。只要是这种情况,它就会记录在 gcc(1) 联机帮助页中。

如您所见,--enable-stdcall-fixup 并非如此,因此您需要明确传递它。为了将任意选项传递给 linker,gcc 有 -Wl。来自 gcc(1):

   -Wl,option
       Pass option as an option to the linker.  [...]

所以在你的情况下,你会打电话给

g++ -Wl,--enable-stdcall-fixup [...]

我没有联机帮助页中提到的 linker 版本,所以它仍然是我无法识别的选项。但是在你的系统上,鉴于 linker 告诉你使用该选项,我只能假设它是识别它的版本。


顺便说一句,当您尝试仅使用一个破折号调用该选项时,您 运行 误入歧途了。您实际上是在使用选项参数 nable-stdcall-fixup 调用我上面提到的 -e 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.

所以你实际上最终传递了一个选项给 linker 说,当你执行你的程序时,你希望它从一个名为 nable-stdcall-fixup 的函数开始执行,而不是通常的 main.