Generate a DEF File From a DLL

Last night I spend multiple hours trying to get a non-broken CSFML build ready, which requires to have import libraries for MSVC and GCC that both depend on the same DLL. This works because the import library only points to the symbols that are in the DLL, thus acts as sort of instruction how to use the DLL.

Now in the past, I’ve been using an ancient tool called LIB2A which was written in Visual Basic and you can find it on some archived Google Code site. The tool still works, but as was pointed out quite a while ago on the SFML forum, the generated DEF files do not contain “typedef-ed” types, such as specific blend modes or specific colors. For CSFML this would mean that if you used the GCC import library, you would be able to use sfBlue or sfBlendNone despite the DLL having these symbols.

So the broken step was generating a DEF  file from a LIB or DLL file. Unfortunately the internet wasn’t very helpful for once and I kept running into weird solutions that didn’t work or tools (e.g. EXPDEF) that were only supporting x86 architecture, I even tried some commercial tool from RAD Studio (MKEXP), but that didn’t work either. I tried different versions of the reimp.exe which is part of MinGW-Utils, but it turns out that was last updated in 2009 (!) and the latest version doesn’t even work.

Only after multiple hours of searching I finally stumbled up on the mentioning of gendef.exe which is shipped with MinGW-w64 distributions and oh look, it’s sole purpose is to generate DEF files from DLLs. With gendef and dlltool, both part of any MinGW-w64 distribution, you can now quite easily generate GCC import libraries from any DLL.

gendef.exe csfml-graphics-2.dll
dlltool.exe -d csfml-graphics-2.def -D csfml-graphics-2.dll -l libcsfml-graphics.a

gendef generates a DEF file in the working directory and dlltool generates a GCC A import library.

I’m mainly putting this information out here, in hopes Google will pick it up and people looking for similar solutions won’t have to spend the same amount of time finding gendef.

12 thoughts on “Generate a DEF File From a DLL

  1. Than you for this article it saved many hours were to lost on same quest
    If you pardon me I have another questons
    “How to to turn *.a to *.lib”
    “How to turn *.a or *.la or both to *.dll”
    is it possible to generate libs (*.a) or dll (I do now what extention those comes with) in Msys2 (mingw64) and use them out with Visual studio and the inverse?

    1. Glad to hear my post could help yet another person! :)

      If your *.a file is an import lib, you should have a DLL from which you can generate a *.def file as shown. With that you should be able to use some MSVC tool to turn the *.def file into a *.lib import library.

      If your *.a file is a static library, then I don’t think you can do much with it, as a static library is nothing more than a collection of object files and object files are compiler/linker specific, so you certainly can’t interchange it. Maybe there’s a way to rebuilt it into a DLL, but I wouldn’t know the process and have to heavily question the use-case here.

  2. Hi and thanks for sharing your expertise! Helpes me a lot. exporting .def from .dll works perfect, but when I use dlltool.exe to generate .a file size is 0 byte. what could be the reason? I use MinGW win32 for i686 although I have a 64 bit system, because I think my .dll is exported for x86 32 bit but I am not sure.
    When I use Lib2A for converting, my .a file is 169 KB from a .dll which is 8.089 KB. Can this be correct?

      1. Yes, running gendef.exe with my .dll worked fine. It created .def file with 4 KB. From that I run dlltool.exe with your command as stated above. does dlltool use some other tools from the MinGW? I copied both tools to my dll folder to use it there. Is there a tool to create the .lib from DLL with gcc instead of Visual Studio?

        1. Hard to say really. You’d need to check what the definition file outputs and if it contains the wanted symbols or not. Also I guess the DLLs needs to have the symbols correctly exported.

          You should be able to run dlltool standalone or at least get an error if something is missing.

  3. This post ALMOST solved my problem. I have a DLL without symbols but I have also *.LIB files which probably should be used because the instructions above didn’t work with the DLL I have (DLL have no symbols so I guess *.LIB files have symbols but gendef.exe libXXX.lib don’t create *.def file from LIB file???

    Any idea how to create *.a file from *.lib file (64bit)?

    1. Assuming it’s a shared library, then you need the DLL to generate the definition file, with which you can create a new import library. If that doesn’t work, then I’m out of ideas as well. Maybe there’s a way to hide the symbols in a DLL?

Leave a Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.