MinGW/GCC Delay Loaded DLL equivalent?
I'm trying to port some old MSVC C++ code to开发者_Python百科 MinGW/GCC.
One problem is that the project relies heavily on the /DELAYLOAD option for functions that aren't always used, and where the proper dll is located at runtime.
Is there such a similar option on MinGW/GCC?
This code is targeting the windows platform.
And I would add that, although delay-load DLLs appear to be a part of the Windows OS, they're actually implemented in terms of small stubs generated by the linker. At least, this used to be the case. So there is no formal notion of "delay loading" at the Windows OS level. There's a convention, based on binary code emitted by the linker.
On elf targets (for Unix-like systems), you can specify the -z lazy
option (which is the default anyway) with ld
(the linker that MinGW also uses).
As far as I know, the i386 PE target (for Windows) does not have an explicit lazy linking option. I can find no documentation of it being available.
You can use the --output-delaylib
argument to dlltool
to create a delay-loaded import library. You can then link against the generated import library to delay load the DLL.
As an example of lazy loading an existing DLL, let's say you want to lazy-load the Windows DLL version.dll
, which exports GetFileVersionInfoSizeW
. First, we define with functions we want to call in a version.def
file:
EXPORTS
GetFileVersionInfoSizeW
We then can create an delay-loaded import library with dlltool
:
dlltool --input-def version.def --output-delaylib version.lib --dllname version.dll
When linking, use the resulting version.lib
:
gcc -o test test.c -lversion -L .
The full version of this example is available on my GitHub.
Fun fact: This feature likely was not available in released software at the time this question was asked. The feature was commit three months before this question was asked.
精彩评论