- 1 year ago
Autotools - Build static and shared libraries with different names
In the Autotools build system (which includes Autoconf, Automake, and Libtool), you can build both static and shared libraries with different names by configuring your project accordingly. Here's how you can achieve this:
Configure.ac (Autoconf):
In yourconfigure.ac
file, you'll define how your libraries should be built. Add the necessary checks and options to determine whether static and shared libraries should be built, and specify the desired library names.shellAC_INIT([Your Project], [1.0])
# Check for static/shared library option (default is shared)
AC_ARG_ENABLE([static],
AS_HELP_STRING([--disable-static], [Build static libraries]),
[enable_static=yes],
[enable_static=no])
# Set library names based on the build type
if test "x$enable_static" = "xyes"; then
LIBRARY_SUFFIX=_static
else
LIBRARY_SUFFIX=_shared
fi
AC_SUBST([LIBRARY_SUFFIX])
# Configure shared libraries
LT_INIT([dlopen])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
Makefile.am (Automake):
In yourMakefile.am
files, you'll define the sources for your libraries and how they should be built.make# Shared library
lib_LTLIBRARIES = libYourLibrary_shared.la
libYourLibrary_shared_la_SOURCES = source1.c source2.c
# Static library
if BUILD_STATIC
lib_LTLIBRARIES += libYourLibrary_static.a
libYourLibrary_static_la_SOURCES = source1.c source2.c
endif
Build the Project:
Run the following commands to generate the necessary build files, configure the project, and build the libraries:shellautoreconf -fvi
./configure --enable-static
make
Replace
--enable-static
with--disable-static
to build the shared libraries instead.
The above steps assume you're using a standard Autotools project structure with Autoconf, Automake, and Libtool. Modify the names and filenames as needed for your project. With this configuration, when you build your project, you'll get different libraries with the specified suffixes (_static
for static libraries and _shared
for shared libraries).
Remember that the Autotools build system might have a learning curve, especially if you're new to it. Make sure to read the documentation and adapt the provided steps to fit your project's structure and needs.