libtool and Windows DLLs
Asked Answered
D

1

6

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])

Driedup answered 22/11, 2013 at 11:9 Comment(2)
I'm having this same exact problem trying to cross-compile a Quake clone on Fedora. Was the upvoted answer below the solution you settled on?Tsarevna
So.. funny story. After hours and hours of headaches with this issue, I just deleted the stupid libSDL*.la libtool turds that building SDL, SDL_image and SDL_ttf from source had installed. And suddenly, everything happily links again. Thanks, libtool. Really. Well done.Tsarevna
S
1

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.

Sinuation answered 28/5, 2014 at 18:37 Comment(5)
Unfortunately you do not know about SDL. The SDL2main.a can not be a shared library, since it provides the main for the application. This is by design and a little annoying.Driedup
Maybe build with your own main() then?Sinuation
Yes, No, Maybe... The awesome thing about the SDL2main library is that you can work with a "normal" main function in a Windows environment without a hitch. (SDL does some setup in a proper WinMain which is sort of needed in a graphical application.)Driedup
Also the entire subject is really annoying. Why is libtool totally incapable building DLLs when using the compiler directly is a piece of cake.Driedup
It's only a piece of cake if you're targeting one build configuration.Sinuation

© 2022 - 2024 — McMap. All rights reserved.