Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libtool and Windows DLLs

I have an difficult relationship with the GNU autotools, especially libtool. But because they kick ass when it comes to portability and cross compilation I started using them again.

Unfortunately I can't get libtool to build proper windows DLLs. Yet with vanilla make gcc will happily build the DLL for me.

For example:

LIBEXT = .dll

pkzo$(LIBEXT): $(patsubst %.cpp, %.o, $(pkzo_SOURCES)) resources.o
    $(CXX) -shared -fPIC $(CXXFLAGS) $^ $(LDFLAGS) -Wl,--out-implib=libpkzo.lib -o $@   

Will haily build a DLL and import library. (Even without any annoying decelspec).

Yet if I use libtool like so:

lib_LTLIBRARIES = libpkzo.la

libpkzo_la_CXXFALGS = ...
libpkzo_la_LDADD    = ...
libpkzo_la_SOURCES  = ...

Libtool comes complaining:

*** Warning: linker path does not have real file for library -lSDL2main.
*** I have the capability to make that library automatically link in when
*** you link to this library.  But I can only do this if you have a
*** shared version of the library, which you do not appear to have
*** because I did check the linker path looking for a file starting
*** with libSDL2main and none of the candidates passed a file format test
*** using a file magic. Last file checked: /usr/local/lib/libSDL2main.a

*** Since this library must not contain undefined symbols,
*** because either the platform does not support them or
*** it was explicitly requested with -no-undefined,
*** libtool will only create a static version of it.

Well guess what libSDL2main.a is a static library and has no DLL.

It there a way to build a DLL with automake not using libtool or telling libtool to stop making a fuss about nothing?

PS: Before anyone mentions it, I am configuring libtool with LT_INIT([shared static win32-dll])

like image 402
rioki Avatar asked Nov 22 '13 11:11

rioki


1 Answers

For the first problem, make sure you have a shared version of the SDL library installed.

If you absolutely must link your DLL to static libs, you can trick libtool into doing it by editing the libtool script. E.g., if you want all of the dependent libs statically linked to your DLL, you can do that by putting this at the end of your configure.ac:

sed -i '/^whole_archive_flag_spec=/s/"$/ \\${wl}-static"/' libtool

Now, this is a gross hack and relies on the particular way that libtool builds command lines right now, so there's no guarantee that it will continue to work---but it does work with libtool 2.4.2. Since you have just one library which you want statically linked, you could achieve that by applying sed a bit differently. Probably you'll want

sed -i '/^whole_archive_flag_spec=/s/"$/ \\${wl}-static \\${wl}-lSDL2main \\${wl}-shared"/' libtool

in order to keep shared linking with whatever other shared libs you have, and then you'll need to take -lSDL2main out of wherever else you have it. This is gimpy, but the thing you're trying to do is also gimpy and libtool isn't made for this.

For the second problem, add -no-undefined to libpkzo_la_LDFLAGS.

like image 91
uckelman Avatar answered Sep 18 '22 16:09

uckelman