Search code examples
pluginsdllmingwlibtool

How do I generate an import lib for an exe using automake/autoconf/libtool?


I am porting a Linux application with plugins to Windows using mingw-w64. I would like to generate an import lib for the main application exe and link the plugin DLLs against this import lib so that plugins can use symbols from the exe. This will allow me to load DLLs on Windows using the dlopen interface in win32-dlfcn.

What I'm not sure how to do is write autoconf, automake, or libtool rules to accomplish this. Does anyone know how to write such rules, or know of another project where these rules are implemented?


Solution

  • It seems that the best way is to go behind libtool's back. Here are some extracts from my Makefile.am.

    Conditionally include the dlfcn.c source. You'll also have to update include path, and possibly place some extern "C" guards in the header file.

    if HOST_MINGW32
    bali_phy_SOURCES += dlfcn-win32/dlfcn.c
    endif
    

    You'll need to adjust the LDFLAGS of the main application so that an import library is generated as a by-product of compilation:

    if HOST_LINUX
    bali_phy_LDFLAGS = -rdynamic
    else
    if HOST_MINGW32
    bali_phy_LDFLAGS = -Wl,--export-all-symbols,--out-implib=libbali-phy.dll.a
    else
    bali_phy_LDFLAGS =
    endif
    endif
    

    To compile the plugin, pass flags directly to the linker in order to avoid explaining the import library to libtool. You'll also have to add the directory containing your main application (here its $(top_builddir)) to the linker search path.

    if HOST_MINGW32
    EXTRADEPENDENCIES = bali-phy.exe
    EXTRALDFLAGS = -L$(top_builddir) -Wl,libbali-phy.dll.a
    else
    EXTRADEPENDENCIES =
    EXTRALDFLAGS =
    endif
    
    mod_la_SOURCES = computation/builtins/mod.C
    mod_la_LDFLAGS = -module -shared -avoid-version -export-dynamic -no-undefined -enable-runtime-pseudo-reloc $(EXTRALDFLAGS)
    mod_la_DEPENDENCIES = $(EXTRADEPENDENCIES)
    

    The _DEPENDENCIES line makes the module depend on the main application, so that the main application and its import library are built first.