3

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?

1 Answer 1

2

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.

1
  • FTR, in case you are using libtool anyway, you can also use libltdl to load the modules. Also, the same --out-implib method also works for a library which loads plugins which in turn use functions from that library. If have put a proof of concept implementation of library-with-plugins at github.com/ndim/ndim-portability-test which solves the linking issues I have always run into in the past (until I read this answer).
    – ndim
    Sep 15, 2017 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.