### Install executable with libtool --mode=install (Ultrix example) Source: https://www.gnu.org/software/libtool/manual/html_node/Installing-executables.html Use this command to install an executable linked against uninstalled libtool libraries. The output shows the actual install command executed by libtool. ```shell a23# libtool --mode=install install -c hell /usr/local/bin/hell install -c hell /usr/local/bin/hell a23# ``` -------------------------------- ### Manual installation of static library with cp Source: https://www.gnu.org/software/libtool/manual/html_node/Installing-libraries.html Shows the traditional way to install a static library on a non-libtool system: copy the library into place and then run ranlib. The footnote warns not to strip static libraries or they will be unusable. ```shell burger$ `su` Password: `********` burger# `cp libhello.a /usr/local/lib/libhello.a` burger# ``` ```shell burger# `ranlib /usr/local/lib/libhello.a` burger# ``` -------------------------------- ### Install static library with libtool install mode using cp Source: https://www.gnu.org/software/libtool/manual/html_node/Installing-libraries.html Uses libtool's install mode with the cp command to install a static library. The output shows the actual commands executed, including copying the library and running ranlib. ```shell a23# `libtool --mode=install cp libhello.la /usr/local/lib/libhello.la` cp libhello.la /usr/local/lib/libhello.la cp .libs/libhello.a /usr/local/lib/libhello.a ranlib /usr/local/lib/libhello.a a23# ``` -------------------------------- ### Libtool linking against installed library Source: https://www.gnu.org/software/libtool/manual/html_node/Linking-executables.html Demonstrates using libtool to link against an installed library, showing how it adds the run-time path flag and dependencies automatically. ```shell burger$ `libtool --mode=link gcc -g -O -o test test.o \ /usr/local/lib/libhello.la` gcc -g -O -o .libs/test test.o -Wl,--rpath \ -Wl,/usr/local/lib /usr/local/lib/libhello.a -lm creating test burger$ ``` -------------------------------- ### Install shared library with libtool install mode using install -c Source: https://www.gnu.org/software/libtool/manual/html_node/Installing-libraries.html Uses libtool's install mode with the install -c command to install a shared library. The output shows the installation of the shared object, the libtool library, and the static library, followed by ranlib. ```shell burger# `libtool --mode=install install -c libhello.la \ /usr/local/lib/libhello.la` install -c .libs/libhello.so.0.0 /usr/local/lib/libhello.so.0.0 install -c libhello.la /usr/local/lib/libhello.la install -c .libs/libhello.a /usr/local/lib/libhello.a ranlib /usr/local/lib/libhello.a burger# ``` -------------------------------- ### Build previous and current release candidates Source: https://www.gnu.org/software/libtool/manual/html_node/Guided-version-info-update.html Build and install the previous release and the current release candidate into separate directories before running the wizard. ```bash $ rm -rf /tmp/prev /tmp/curr $ tar xfz libfoo-1.4.tar.gz $ (cd libfoo-1.4 && ./configure --prefix=/tmp/prev && make && make install) $ tar xfz libfoo-1.4.99.tar.gz $ (cd libfoo-1.4.99 && ./configure --prefix=/tmp/curr && make && make install) ``` -------------------------------- ### Get post-installation hints with libtool finish mode Source: https://www.gnu.org/software/libtool/manual/html_node/Installing-libraries.html Runs libtool -n finish to display hints on what to do after installing libraries, such as setting LD_LIBRARY_PATH or using the -L flag during linking. The -n flag performs a dry run. ```shell burger# `libtool -n finish /usr/local/lib` PATH="$PATH:/sbin" ldconfig -m /usr/local/lib ----------------------------------------------------------------- Libraries have been installed in: /usr/local/lib To link against installed libraries in a given directory, LIBDIR, you must use the '-LLIBDIR' flag during linking. You will also need to do one of the following: - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the 'LD_RUN_PATH' environment variable during linking - use the '-RLIBDIR' linker flag See any operating system documentation about shared libraries for more information, such as the ld and ld.so manual pages. ----------------------------------------------------------------- burger# ``` -------------------------------- ### Manual linking against installed library Source: https://www.gnu.org/software/libtool/manual/html_node/Linking-executables.html Shows the manual way to link against an already-installed library using gcc with -L and -l flags. ```shell burger$ `gcc -g -O -o test test.o -L/usr/local/lib -lhello -lm` ``` -------------------------------- ### Install executable with libtool --mode=install (wrapper script system) Source: https://www.gnu.org/software/libtool/manual/html_node/Installing-executables.html On shared library systems that require wrapper scripts, libtool ignores the wrapper script and installs the correct binary from .libs. The output shows the install command targeting .libs/hell. ```shell burger# libtool --mode=install install -c hell /usr/local/bin/hell install -c .libs/hell /usr/local/bin/hell burger# ``` -------------------------------- ### configure.ac: Embed installable libltdl with LTDL_INIT Source: https://www.gnu.org/software/libtool/manual/html_node/Distributing-libltdl.html Shows the configure.ac fragment to embed an installable libltdl. It sets the subdirectory with LT_CONFIG_LTDL_DIR, enables dlopen support with LT_INIT([dlopen]), and enables the installable library with LTDL_INIT([installable]). Replace 'installable' with 'convenience' for the convenience variant. ```autoconf ... # Name the subdirectory that contains libltdl sources LT_CONFIG_LTDL_DIR([libltdl]) # Configure libtool with dlopen support if possible LT_INIT([dlopen]) # Enable building of the installable libltdl library LTDL_INIT([installable]) ... ``` -------------------------------- ### Configure command with --enable-shared=bfd,gdb Source: https://www.gnu.org/software/libtool/manual/html_node/LT_005fINIT.html Example of running configure with --enable-shared=bfd,gdb to build shared libraries for the packages 'bfd' and 'gdb' but not for others like 'libg++'. ```shell trick$ ./configure --enable-shared=bfd,gdb ``` -------------------------------- ### foo.lo on static-only system Source: https://www.gnu.org/software/libtool/manual/html_node/Creating-object-files.html Example .lo file generated by libtool on a system that only supports static libraries. The pic_object is set to 'none'. ```text # foo.lo - a libtool object file # Generated by ltmain.sh (GNU libtool) 2.6.2 # # Please DO NOT delete this file! # It is necessary for linking the library. # Name of the PIC object. pic_object=none # Name of the non-PIC object. non_pic_object='foo.o' ``` -------------------------------- ### Intrinsics module Makefile.am configuration Source: https://www.gnu.org/software/libtool/manual/html_node/Linking-with-dlopened-modules.html Example configuration for a module in a sibling directory, including a rule to build the required interface library. ```Makefile AM_CPPFLAGS = -I$(srcdir)/../libloader AM_LDFLAGS = -no-undefined -module -avoid-version \ -export-dynamic noinst_LTLIBRARIES = intrinsics.la intrinsics_la_LIBADD = ../libloader/libinterface.la ../libloader/libinterface.la: cd ../libloader && $(MAKE) $(AM_MAKEFLAGS) libinterface.la ``` -------------------------------- ### Extract demo files from Autotest Source: https://www.gnu.org/software/libtool/manual/html_node/Using-libtool.html Run the demo test with specific flags to preserve artifacts, then copy the test directory to a demo folder. This provides the source files for the examples in this chapter. ```shell burger$ make check TESTSUITEFLAGS="-d -k 'preloaded static library'" burger$ cp -r tests/testsuite.dir/027 demo/ ``` -------------------------------- ### libtool --reorder-cache examples (second set) Source: https://www.gnu.org/software/libtool/manual/html_node/Invoking-libtool.html Shows the effect of --reorder-cache when the previously preferred directory is removed: the reordered list omits the removed directory and appends the remaining original directories. ```shell $ `libtool --reorder-cache=/tmp/testing` Original: /usr/lib /usr/X11R6/lib /usr/local/lib Reordered: /tmp/testing /usr/lib /usr/X11R6/lib /usr/local/lib $ `rm -rf /tmp/testing` $ `libtool --reorder-cache=/usr/lib:/usr/X11R6/lib:/usr/local/lib` Original: /tmp/testing /usr/lib /usr/X11R6/lib /usr/local/lib Reordered: /usr/lib /usr/X11R6/lib /usr/local/lib ``` -------------------------------- ### Iterating over module handles with lt_dlhandle_iterate Source: https://www.gnu.org/software/libtool/manual/html_node/User-defined-module-data.html Example loop that iterates over all module handles matching the given interface, starting with a NULL handle and continuing until lt_dlhandle_iterate returns NULL. ```c lt_dlhandle handle = 0; lt_dlinterface_id iface = my_interface_id; while ((handle = lt_dlhandle_iterate (iface, handle))) { ... } ``` -------------------------------- ### my_dlopenext: emulate lt_dlopenext using lt_dlopenadvise Source: https://www.gnu.org/software/libtool/manual/html_node/Libltdl-interface.html Shows how to initialize an lt_dladvise, set the ext hint, and call lt_dlopenadvise to emulate lt_dlopenext. The advise object is destroyed after use. On failure, lt_dladvise_init or lt_dladvise_ext returns non-zero and sets an error retrievable with lt_dlerror. ```c lt_dlhandle my_dlopenext (const char *filename) { lt_dlhandle handle = 0; lt_dladvise advise; if (!lt_dladvise_init (&advise) && !lt_dladvise_ext (&advise)) handle = lt_dlopenadvise (filename, advise); lt_dladvise_destroy (&advise); return handle; } ``` -------------------------------- ### Compile foo.c and hello.c with gcc Source: https://www.gnu.org/software/libtool/manual/html_node/Creating-object-files.html Creating object files for static library sources foo.c and hello.c. ```shell burger$ `gcc -I. -g -O -c foo.c` burger$ `gcc -I. -g -O -c hello.c` burger$ ``` -------------------------------- ### Using macros in foo.h Source: https://www.gnu.org/software/libtool/manual/html_node/C-header-files.html Shows how to use the BEGIN_C_DECLS, END_C_DECLS, and PARAMS macros in a header file, with an include guard and a placeholder for the macro definitions. ```C #ifndef FOO_H #define FOO_H /* The above macro definitions. */ #include "..." BEGIN_C_DECLS int foo PARAMS((void)); int hello PARAMS((void)); END_C_DECLS #endif /* !FOO_H */ ``` -------------------------------- ### lt_dlinit Source: https://www.gnu.org/software/libtool/manual/html_node/Libltdl-interface.html Initializes libltdl. Must be called before using libltdl and may be called several times. Returns 0 on success, otherwise the number of errors. ```APIDOC ## lt_dlinit ### Description Initialize libltdl. This function must be called before using libltdl and may be called several times. Return 0 on success, otherwise the number of errors. ### Function Signature `int lt_dlinit (void)` ### Parameters None. ### Return Value - **int** - 0 on success, otherwise the number of errors. ``` -------------------------------- ### configure.ac: Check for installed libltdl version using AC_CHECK_LIB Source: https://www.gnu.org/software/libtool/manual/html_node/Distributing-libltdl.html Use this configure.ac snippet after LTDL_INIT to verify that an installed libltdl is recent enough, by checking for the lt_dladvise_init symbol. It sets CFLAGS and LDFLAGS to include the libltdl headers and library, and aborts with an error if the installed libltdl is too old. ```autoconf LT_INIT([dlopen]) LTDL_INIT # The lt_dladvise_init symbol was added with libtool-2.2 if test yes != "$with_included_ltdl"; then save_CFLAGS=$CFLAGS save_LDFLAGS=$LDFLAGS CFLAGS="$CFLAGS $LTDLINCL" LDFLAGS="$LDFLAGS $LIBLTDL" AC_CHECK_LIB([ltdl], [lt_dladvise_init], [], [AC_MSG_ERROR([installed libltdl is too old])]) LDFLAGS=$save_LDFLAGS CFLAGS=$save_CFLAGS fi ``` -------------------------------- ### Passing options to libtoolize via LIBTOOLIZE_OPTIONS Source: https://www.gnu.org/software/libtool/manual/html_node/Invoking-libtoolize.html Example of using the LIBTOOLIZE_OPTIONS environment variable with autoreconf to pass options to libtoolize. ```shell trick$ `LIBTOOLIZE_OPTIONS=--no-warn,--quiet autoreconf --install` ``` -------------------------------- ### Set up Makefile.am for nonrecursive libltdl build Source: https://www.gnu.org/software/libtool/manual/html_node/Distributing-libltdl.html Add these variable initializations and the include of libltdl/ltdl.mk near the top of Makefile.am. If other libraries are built, change lib_LTLIBRARIES to use '+=' so the libltdl targets are not overwritten. ```makefile AM_CPPFLAGS = AM_LDFLAGS = BUILT_SOURCES = EXTRA_DIST = CLEANFILES = MOSTLYCLEANFILES = include_HEADERS = noinst_LTLIBRARIES = lib_LTLIBRARIES = EXTRA_LTLIBRARIES = include libltdl/ltdl.mk ``` -------------------------------- ### lt_dlopenadvise Source: https://www.gnu.org/software/libtool/manual/html_node/Libltdl-interface.html Opens a module with additional hints provided by an advise object. The advise parameter is opaque and must be initialized with lt_dladvise_init before use. ```APIDOC ## lt_dlopenadvise ### Description Opens a module with additional hints provided by an advise object. The advise parameter is opaque and must be initialized with lt_dladvise_init before use. ### Function Signature `lt_dlhandle lt_dlopenadvise (const char *filename, lt_dladvise advise)` ### Parameters - **filename** (const char *) - Required - The name of the module to open. - **advise** (lt_dladvise) - Required - An opaque advice object containing hints for the module loader. Must be initialized with lt_dladvise_init before use. ### Returns - **lt_dlhandle** - A handle to the loaded module, or NULL on failure. Use lt_dlerror to retrieve error message. ### Notes - The advise parameter is passed by value, not by pointer, and is not modified by this function. ``` -------------------------------- ### Enable C++ support using LT_LANG Source: https://www.gnu.org/software/libtool/manual/html_node/LT_005fINIT.html Example showing how to add C++ language support to Libtool using the LT_LANG macro. ```m4 LT_INIT LT_LANG([C++]) ``` -------------------------------- ### foo.h header with include guard and extern "C" Source: https://www.gnu.org/software/libtool/manual/html_node/C-header-files.html Example from foo.h showing a header file that uses an include guard (#ifndef FOO_H) and extern "C" blocks to be compatible with C++ and C compilers. ```C #ifndef FOO_H #define FOO_H #ifdef __cplusplus extern "C" { #endif int foo (void); int hello (void); #ifdef __cplusplus } #endif #endif /* !FOO_H */ ``` -------------------------------- ### foo.c - Library implementation Source: https://www.gnu.org/software/libtool/manual/html_node/Windows-DLLs.html Implementation file that defines the functions and variable declared in foo.h. It includes the header and provides the actual logic. ```c #include "foo.h" int one (void) { return 1; } int two (void) { return three - one (); } int three = 3; ``` -------------------------------- ### Enable C++ support using AC_PROG_CXX Source: https://www.gnu.org/software/libtool/manual/html_node/LT_005fINIT.html Example showing how to add C++ language support to Libtool using the Autoconf AC_PROG_CXX macro. ```m4 LT_INIT AC_PROG_CXX ```