Autotools - 使用不同的名称构建静态和共享库

时间:2018-01-07 00:34:57

标签: linux windows shared-libraries static-libraries autotools

Windows 上使用 cmake 构建两个静态和共享库时,我必须将“-static”附加到静态库名称(开启) Windows,静态共享库都可以具有.lib扩展名),因此静态库 lib 的结果文件名是 lib-static.lib 。 如何使用 automake + libtool (即指示libtool在其运行的任何系统上将“ -static ”附加到生成的静态库文件名中)?

1 个答案:

答案 0 :(得分:4)

在带有libtool的Autotools系统中,如果同时构建静态库和共享库,它们通常都与相同的libtool库目标相关联,因此它们具有相同的名称,仅在在扩展。这通常不是问题,因为UNIX样式的约定对这些使用不同的扩展。实际上,它针对UNIX约定运行,以提供具有不同名称的静态和共享库。

如果你坚持创建不同名称的静态和共享库,那么最好的办法就是为它们定义不同的目标。在最坏的情况下,您可以简单地构建所有源代码两次:

lib_LTLIBRARIES = libfoo.la libfoo-static.la

libfoo_la_SOURCES = source1.c source2.c
# Build *only* a shared library:
libfoo_la_LDFLAGS = -shared

# use the same sources as the shared version:
# note: the underscore in "libfoo_static" is not a mistake
libfoo_static_la_SOURCES = $(libfoo_la_SOURCES)
# Build *only* a static library:
libfoo_static_la_LDFLAGS = -static

但是,如果您想避免对源进行多次编译,那么您应该能够创建一个便利库"从中创建两个库:

noinst_LTLIBRARIES = libfoo_funcs.la
lib_LTLIBRARIES = libfoo.la libfoo-static.la

# A convenience library (because noinst)
# Build both static and shared versions
libfoo_funcs_la_SOURCES = source1.c source2.c

# Build *only* a shared library
libfoo_la_LDFLAGS = -shared
# Include the contents of libfoo_funcs.la
libfoo_la_LIBADD = libfoo_funcs.la

# Build *only* a static library
libfoo_static_la_LDFLAGS = -static
# Include the contents of libfoo_funcs.la
libfoo_static_la_LIBADD = libfoo_funcs.la

如果可以组合相同的对象以首先创建静态库和共享库,那么这只会有助于避免多次编译源。在某些平台上就是这种情况,但在其他平台上并非如此。