7.2.  Exercise: Installing Libraries

[ fromfile: installing-libs.xml id: installing-libs ]

Abstract

A number of examples in this book make use of classes found in libraries that were written for this book. The source code for these classes is available for download. API documentation is included, generated with Doxygen. In this exercise, you can see how to build and install some libraries.

Instructions for installing libraries on a *nix platform for use with the book examples follow. For help installing Qt and MySQL on Windows with MinGW check the QtCentre Wiki.

As we suggested in Section 7.1 and Section 7.1.2:

[Note]Note

If you decide to comment out a particular library directory in libs.pro, you should also comment out the corresponding test dir in libs/tests/tests.pro (or else the tests part of libs won't build).

Reminder: You can comment out any line in a project file by inserting the poundsign char '#' at the beginning of that line.

libs.pro

  TEMPLATE = subdirs
  CPPLIBS=$$(CPPLIBS)
  isEmpty(CPPLIBS) {
     error("Define CPPLIBS environment variable to point to this location.")
  }
  SUBDIRS += dataobjects \
             actioneditor \
             customer \
             metadata \
             sqlmetadata
  

Build the libraries from the libs directory in two steps:

  1. qmake -recursive // creates Makefiles in current dir and in each subdir.

  2. make // builds the libraries and tests.[56]

Verify that the libraries are built and that the shared object files (e.g., libdataobjects.so [57]) are located in the CPPLIBS directory. Following is an abbreviated directory listing from a typical linux box:

libs> ls -l
drwxr-xr-x  5 dataobjects
lrwxrwxrwx  1 libactioneditor.so -> libactioneditor.so.1.0.0
lrwxrwxrwx  1 libactioneditor.so.1 -> libactioneditor.so.1.0.0
lrwxrwxrwx  1 libactioneditor.so.1.0 -> libactioneditor.so.1.0.0
-rwxrwxr-x  1 libactioneditor.so.1.0.0
[...]
lrwxrwxrwx  1 libdataobjects.so -> libdataobjects.so.1.0.0
lrwxrwxrwx  1 libdataobjects.so.1 -> libdataobjects.so.1.0.0
lrwxrwxrwx  1 libdataobjects.so.1.0 -> libdataobjects.so.1.0.0
-rwxr-xr-x  1 libdataobjects.so.1.0.0
-rw-r--r--  1 libs.pro
-rw-r--r--  1 Makefile
libs> 

Lines that begin with drwxr-xr-x are directories. Lines that begin with lrwxrwxrwx are symbolic links. Use Google to find out why each shared object file has three symbolic links.

Fixing the Linker Path

[Note]Note

On a *nix platform a shell script is generally used to define environment variables. Example 7.3 shows a bash shell script that handles the job.

Example 7.3. src/bash/env-script.sh

export CPPLIBS=$HOME/oop/projects/libs
export LD_LIBRARY_PATH=$CPPLIBS

<include src="src/bash/env-script.sh" href="src/bash/env-script.sh" id="env-script" mode="code"/>


Note the bash syntax details in this script:

  • The environment variable HOME contains the absolute path to your personal "home" directory. You can also use the tilde symbol ~ for this.

  • An environment variable on the left of an assignment has no dollar sign $ prefix.

  • An environment variable on the right of an assignment must have a $ prefix.

  • The command export is needed if the environment variable is to become a part of the environment – and not simply be local to the script file.

You can run this script by typing one of the following commands:

   source env-script.sh
      or
   . env-script.sh
   

Notice the dot (.) at the beginning of the second version. In the bash shell, the dot is equivalent to the command, source.

If you want to make sure that these environment variables are automatically set at the start of each shell, you can source the script from ~/.bashrc, which runs automatically whenever bash starts (for example, whenever you bring up a terminal or console).

Hamish Whittal has put together a nice online course on Shell Scripting.



[56] You may need to install some specialized Qt packages before building some of our libraries. For example, the libphonon-dev package is required for our phononmetadata library. The linker will inform you of these dependencies.

[57] Or on Windows, libdataobjects.lib and dataobjects.dll, the latter which must be in a directory listed in your PATH, so be sure to include %CPPLIBS% in your PATH.