Autotools - 构建具有不同名称的静态和共享库

Autotools - Build static and shared libraries with different names

Windows 上使用 cmake 构建 静态库和共享库时我必须将“-static”附加到静态库名称(在 Windows 上,静态和共享库都可以有 .lib 扩展名)所以静态库的结果文件名 liblib-static.lib。 如何在任何系统上使用 automake+libtool(即指示 libtool 将“-static”附加到生成的静态库文件名)来实现这一点运行 在吗?

在具有 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

但是,如果您想避免多次编译您的源代码,那么您应该能够创建一个 "convenience library" 从中创建两个库:

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

如果可以首先组合相同的对象来同时创建静态库和共享库,那只会有助于避免对源代码进行多次编译。有的平台是这样,有的平台不是这样。