Cyberithub

How to Install libtool on Ubuntu or Debian Linux

Advertisements

In this article, we will see how to install libtool on Ubuntu or Debian Linux. If you are a linux software developer and looking for an interface which can take care of the process of compiling and linking shared library so that you can focus more on your project and less on systems library management then libtool is the one you should go for. In fact, if you are working with software that uses GNU build tools like autoconf and automake then installation of libtool is very much required in your system.

Libtool not only simplifies the whole build process but also manages the versioning of shared libraries thus ensuring the backward compatibility. Using libtool, you can also test the version of libraries without the need to install them. This makes it easy for developers to test different versions. Check more about libtool on GNU official website. Here we will see the steps to install libtool on Ubuntu or Debian based systems.

 

How to Install libtool on Ubuntu or Debian Linux

How to Install libtool on Ubuntu or Debian Linux

Also Read: How to Install libtool on Ubuntu 22.04

Step 1: Prerequisites

a) You would require a running Ubuntu or Debian based Linux system.

b) You would also require sudo or root access to run update and install libtool package.

c) You would need a default Ubuntu package manager such as apt or apt-get to install the package from repo.

 

Step 2: Update Your Server

Often, it has been told to check and install all the latest available critical security patches and feature upgrades before installing any new package in your system. This is simply to avoid any compatibility issues with the package dependencies as well as to avoid any potential vulnerability in the system and at the same time to take advantage of recent updates for better performance. All these can be achieved by running sudo apt update && sudo apt upgrade command as shown below.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade

 

Step 3: Install libtool

You can use either apt or aptitude to install libtool in your ubuntu or debian based system. We will see both the method here.

a) Using Apt

If you have apt utility available in your system then you can install libtool by using sudo apt install libtool command as shown below. This will install the package along with all its dependencies from default Ubuntu repo.

cyberithub@ubuntu:~$ sudo apt install libtool

b) Using Aptitude

If you have aptitude utility available in your system then you can install libtool by using sudo aptitude install libtool command as shown below.

cyberithub@ubuntu:~$ sudo aptitude install libtool

 

Step 4: Using libtool

Now that libtool is installed, let's see a simple demonstration of how you use libtool to create a shared library on a Ubuntu or Debian based Linux system. Before proceeding with the demonstration, let's first understand the project structure. For our example, we are using below structure:-

cyberithub@ubuntu:~$ tree demoproject/
demoproject/
├── configure.ac
├── Makefile.am
└── src
    ├── test.c
    └── test.h

1 directory, 4 files

Here are brief description of all the project files:-

  • test.c contains the source code for a function you wish to compile into a shared library.
  • test.h is the header file for your function.
  • configure.ac and Makefile.am are used by Autoconf and Automake, respectively, to configure and generate the Makefiles for your project.

In the next step, specify the source code in test.c as follows:-

#include "test.h"
#include <stdio.h>

void test(const char *name) {
    printf("CyberITHub, %s!\n", name);
}

Then specify header file(test.h) contents as follow:-

#ifndef TEST_H
#define TEST_H

void test(const char *name);

#endif

You would also need a autoconf script(configure.ac) which might look like below:-

AC_INIT([TestLib], [0.1], [your-email@example.com])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
AM_PROG_AR
LT_INIT
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Here are the complete details of above code:-

a) AC_INIT([TestLib], [0.1], [your-email@example.com])

AC_INIT is the first macro in configure.ac and initializes the configuration process.

  • [TestLib] is the name of the software package.
  • [0.1] specifies the version of the package.
  • [your-email@example.com] is the email address where users can report bugs.

b) AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])

AM_INIT_AUTOMAKE initializes Automake, which is another tool from the GNU build system used to generate Makefile.in files from Makefile.am templates.[-Wall -Werror foreign subdir-objects] are options passed to Automake:-

  • -Wall enables all warning messages.
  • -Werror treats warnings as errors, forcing them to be addressed.
  • foreign relaxes some GNU standards, allowing more flexibility in the files required in the root directory of the package.
  • subdir-objects instructs Automake to compile source files in subdirectories into object files in the same subdirectories, avoiding filename conflicts.

c) AM_PROG_AR

  • AM_PROG_AR checks for a suitable archiver (ar) program to create static libraries. It's necessary for packages that include static library creation.

d) LT_INIT

  • LT_INIT initializes Libtool, which is used to abstract the complexity of using shared and static libraries across various platforms. It sets up the necessary infrastructure to use Libtool within the package.

e) AC_CONFIG_FILES([Makefile])

  • AC_CONFIG_FILES specifies which input files configure should use to generate output files. Here, it's told to generate a Makefile from Makefile.in. Makefile.in is itself generated by Automake from Makefile.am.

f) AC_OUTPUT

  • AC_OUTPUT generates the output files, concluding the configure script generation. It takes the input files specified by AC_CONFIG_FILES and other macros and processes them to produce the final configuration scripts and makefiles.

Then you need to configure automake(Makefile.am) file which will specify how to build the library. In below code, we are telling automake to build a library libtest.la from src/test.c.

lib_LTLIBRARIES = libtest.la
libtest_la_SOURCES = src/test.c
libtest_la_LDFLAGS = -version-info 1:0:0

To generate configure script and Makefile.in, switch to demoproject directory and run autoreconf --install command as shown below. You can ignore the recommendation for now.

cyberithub@ubuntu:~/demoproject$ autoreconf --install
libtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.ac,
libtoolize: and rerunning libtoolize and aclocal.
libtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.

You will see configure script gets generated. Now run configure script to prepare for build in your system.

cyberithub@ubuntu:~/demoproject$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
.............................................
.............................................
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
config.status: executing libtool commands

Then compile the code by running make utility as shown below. This utility will read the compilation instructions from Makefile in current directory and compile source code accordingly.

cyberithub@ubuntu:~/demoproject$ make
depbase=`echo src/test.lo | sed 's|[^/]*$|.deps/&|;s|\.lo$||'`;\
/bin/bash ./libtool --tag=CC --mode=compile gcc -DPACKAGE_NAME=\"TestLib\" -DPACKAGE_TARNAME=\"testlib\" -DPACKAGE_VERSION=\"0.1\" -DPACKAGE_STRING=\"TestLib\ 0.1\" -DPACKAGE_BUGREPORT=\"your-email@example.com\" -DPACKAGE_URL=\"\" -DPACKAGE=\"testlib\" -DVERSION=\"0.1\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -I. -g -O2 -MT src/test.lo -MD -MP -MF $depbase.Tpo -c -o src/test.lo src/test.c &&\
mv -f $depbase.Tpo $depbase.Plo
libtool: compile: gcc -DPACKAGE_NAME=\"TestLib\" -DPACKAGE_TARNAME=\"testlib\" -DPACKAGE_VERSION=\"0.1\" "-DPACKAGE_STRING=\"TestLib 0.1\"" -DPACKAGE_BUGREPORT=\"your-email@example.com\" -DPACKAGE_URL=\"\" -DPACKAGE=\"testlib\" -DVERSION=\"0.1\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -I. -g -O2 -MT src/test.lo -MD -MP -MF src/.deps/test.Tpo -c src/test.c -fPIC -DPIC -o src/.libs/test.o
libtool: compile: gcc -DPACKAGE_NAME=\"TestLib\" -DPACKAGE_TARNAME=\"testlib\" -DPACKAGE_VERSION=\"0.1\" "-DPACKAGE_STRING=\"TestLib 0.1\"" -DPACKAGE_BUGREPORT=\"your-email@example.com\" -DPACKAGE_URL=\"\" -DPACKAGE=\"testlib\" -DVERSION=\"0.1\" -DHAVE_STDIO_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_STRINGS_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_UNISTD_H=1 -DSTDC_HEADERS=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -I. -g -O2 -MT src/test.lo -MD -MP -MF src/.deps/test.Tpo -c src/test.c -o src/test.o >/dev/null 2>&1
/bin/bash ./libtool --tag=CC --mode=link gcc -g -O2 -version-info 1:0:0 -o libtest.la -rpath /usr/local/lib src/test.lo
libtool: link: gcc -shared -fPIC -DPIC src/.libs/test.o -g -O2 -Wl,-soname -Wl,libtest.so.1 -o .libs/libtest.so.1.0.0
libtool: link: (cd ".libs" && rm -f "libtest.so.1" && ln -s "libtest.so.1.0.0" "libtest.so.1")
libtool: link: (cd ".libs" && rm -f "libtest.so" && ln -s "libtest.so.1.0.0" "libtest.so")
libtool: link: ar cr .libs/libtest.a src/test.o
libtool: link: ranlib .libs/libtest.a
libtool: link: ( cd ".libs" && rm -f "libtest.la" && ln -s "../libtest.la" "libtest.la" )

Finally install the generated binaries by using sudo make install command as shown below.

cyberithub@ubuntu:~/demoproject$ sudo make install
[sudo] password for cyberithub:
make[1]: Entering directory '/home/cyberithub/demoproject'
/usr/bin/mkdir -p '/usr/local/lib'
/bin/bash ./libtool --mode=install /usr/bin/install -c libtest.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libtest.so.1.0.0 /usr/local/lib/libtest.so.1.0.0
libtool: install: (cd /usr/local/lib && { ln -s -f libtest.so.1.0.0 libtest.so.1 || { rm -f libtest.so.1 && ln -s libtest.so.1.0.0 libtest.so.1; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libtest.so.1.0.0 libtest.so || { rm -f libtest.so && ln -s libtest.so.1.0.0 libtest.so; }; })
libtool: install: /usr/bin/install -c .libs/libtest.lai /usr/local/lib/libtest.la
libtool: install: /usr/bin/install -c .libs/libtest.a /usr/local/lib/libtest.a
libtool: install: chmod 644 /usr/local/lib/libtest.a
libtool: install: ranlib /usr/local/lib/libtest.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least 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 '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
make[1]: Nothing to be done for 'install-data-am'.
make[1]: Leaving directory '/home/cyberithub/demoproject'

To check library is installed correctly or not, you can run ls /usr/local/lib/libtest* command as shown below.

cyberithub@ubuntu:~/demoproject$ ls /usr/local/lib/libtest*
/usr/local/lib/libtest.a /usr/local/lib/libtest.la /usr/local/lib/libtest.so /usr/local/lib/libtest.so.1 /usr/local/lib/libtest.so.1.0.0

 

Step 5: Uninstall libtool

If you are not going to use libtool anymore, you can think of removing it from your system by using any of the below methods.

a) Using Apt

You can easily remove libtool from your system by running sudo apt remove libtool command as shown below. To remove all the dependencies along with the package, use --auto-remove option with below command.

cyberithub@ubuntu:~$ sudo apt remove libtool

If you are looking to remove libtool package along with all its dependencies and related configuration files then you can use sudo apt purge libtool --auto-remove command as shown below.

cyberithub@ubuntu:~$ sudo apt purge libtool --auto-remove

b) Using Aptitude

You can also use aptitude to remove libtool package from your system. You just have to run sudo aptitude remove libtool command as shown below.

cyberithub@ubuntu:~$ sudo aptitude remove libtool

Leave a Comment