How to link to an xxx.dll when the provider only ships it with an xxx.lib and you are using MinGW-w64 and not MSVC?

こ雲淡風輕ζ 提交于 2020-02-23 07:50:29

问题


Firebird and Boost communities only provide the xxx.lib for their pre-built xxx.dll binaries,

and I am using MinGW-w64 v7.0.0 with GCC v8.1.0,

and this last one expects a libxxx.a file containing all the xxx.dll function symbols to link with.

For Boost, I can build it from the source code for MinGW-w64 (though I still prefer using the pre-built ones, because the build process for big toolkits like this one takes forever).

As for Firebird, it is not buildable using MinGW-w64 at all, except if patched, and I can't guarantee that the patched source files will produce a safe fbclient.dll for production.

And I have heard that newer versions of MinGW-w64 accept direct linking to a .dll,

Is this true? is it just MinGW-w64 .dlls, only C .dlls or including C++ ones, or any kind of a .dll including MSVC ones.

You may say: "Why don't I just use Msys2!?"

  1. Msys2 is a patch based software, and I have seen a lot of libraries like libcurl that are still not purely static (the static version, the .a one not the .dll.a one) still depends on one or more external .dlls (not the system ones).
  2. Msys2 is Dwarf2 based exception handling and I am using the sjlj one.
  3. Msys2 uses a very recent tool-chain v9.2.0 and libraries like winpthreads, libwinpthread and zstd that are no longer supported in Windows XP (Windows XP!? YES, more of my clients are still using it), so I have to downgrade the tool-chain.

So, what are my options, for linking here?

TIA.


回答1:


Windows DLLs can not be linked with a executable programs. For linking, we have to create a export library for mingw-w64. This answer assumes you have already install mingw-w64 in your development environment and mingw-w64 tools is accessible in command line.

The export library can be created from DLL or LIB file. Here the procedure is followed from DLL file. Open command prompt. Assume the DLL is foo.dll.

  • Generate Export Definition file with gendef:
gendef.exe foo.dll

This command takes foo.dll and generates foo.def.

  • Generate export library with dlltool:
dlltool.exe --dllname foo.dll --input-def foo.def --output-lib libfoo.a

This command takes foo.def and foo.dll and generates libfoo.a. Remember the export library libfoo.a name starts with lib- but at the time of compiling/linking the linking option should be -lfoo in case of GCC. The generated export library libfoo.a only contains the symbols that are exported from the DLL, it can not be used for static linking.

The procedure does not depend if the host is MSYS2 or MinGW-w64 or cross compiler in any GNU/Linux distribution.

Further Readings

  • mingw: How to create an import library for a DLL using MinGW
  • mingw-w64: Generating an import library for a DLL
  • dlltool manpage


来源:https://stackoverflow.com/questions/60068235/how-to-link-to-an-xxx-dll-when-the-provider-only-ships-it-with-an-xxx-lib-and-yo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!