Libtool和Windows DLLs

6
我和GNU Autotools(特别是Libtool)的关系很麻烦,但由于它们在可移植性和交叉编译方面表现出色,所以我又开始使用它们了。
不幸的是,我无法让Libtool构建出正确的Windows DLL。但是,使用原始的make和gcc,可以轻松地为我构建DLL。
例如:
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)."
"但是,如果我使用libtool这样做:"
lib_LTLIBRARIES = libpkzo.la

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

Libtool 抱怨道:
*** 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.

“libSDL2main.a是一个静态库,没有DLL。有没有一种方法可以使用automake构建DLL而不使用libtool或告诉libtool停止大惊小怪?PS:在任何人提到它之前,我正在使用LT_INIT([shared static win32-dll])配置libtool。”

我也遇到了同样的问题,试图在 Fedora 上交叉编译一个 Quake 克隆版本。下面得到赞同票的答案是你采用的解决方案吗? - jdolan
所以...有个有趣的故事。在经历了数小时的头痛之后,我终于删除了从源代码构建SDL、SDL_image和SDL_ttf时安装的愚蠢的libSDL*.la libtool垃圾文件。然后,一切都可以愉快地链接了。感谢你,libtool。真的,干得好。 - jdolan
1个回答

1
对于第一个问题,请确保您安装了共享版本的SDL库。
如果您一定要将DLL链接到静态库,您可以通过编辑libtool脚本来欺骗libtool。例如,如果您希望将所有依赖库静态链接到您的DLL中,则可以在configure.ac的末尾添加以下内容:
sed -i '/^whole_archive_flag_spec=/s/"$/ \\${wl}-static"/' libtool

现在,这是一个粗糙的 hack,并且依赖于 libtool 目前构建命令行的特定方式,因此不能保证它将继续工作 - 但它可以与 libtool 2.4.2 一起使用。由于您只有一个要静态链接的库,因此可以通过以不同的方式应用 sed 来实现。可能您希望。
sed -i '/^whole_archive_flag_spec=/s/"$/ \\${wl}-static \\${wl}-lSDL2main \\${wl}-shared"/' libtool

为了保持与其他共享库的链接,您需要将-lSDL2main从其他位置中移除。这很麻烦,但您尝试做的事情也很麻烦,而且libtool并不适用于此。
对于第二个问题,请将-no-undefined添加到libpkzo_la_LDFLAGS中。

很遗憾,您不了解SDL。SDL2main.a不能是共享库,因为它为应用程序提供了主函数。这是设计上的限制,有点令人烦恼。 - rioki
也许可以自己编写 main() 函数来构建? - uckelman
是,不是,可能…… SDL2main库的精妙之处在于,您可以在Windows环境中使用“正常”的main函数而不会出现任何问题。(SDL在适当的WinMain中进行了一些设置,这在图形应用程序中有点必要。) - rioki
1
整个主题真的很烦人。为什么使用编译器直接构建DLL非常容易,而libtool却完全无法做到呢? - rioki
只有当你针对一个构建配置时,这才是小菜一碟。 - uckelman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接