Reusing Other Libraries

Many of our examples link with various libraries that we have supplied. The one we use most frequently is called libutils. You can download a tarball containing this library here.[1] Create a shell/environment variable CPPLIBS that points to a convenient (empty) directory and then unpack the utils tarball in that directory.

[1] http://oop.mcs.suffolk.edu/dist

When we set up projects that reuse utils we will always assume that the shell/environment variable $CPPLIBS (or %CPPLIBS% in Windows) has been properly set to contain the "libs root." This variable is used for two purposes: It is the parent directory of all the C++ source code for libraries supplied by us (or by you), and it is also the destination directory of the compiled shared object code for those libraries.

qmake can access an environment variable such as CPPLIBS from inside a project file using the syntax $$(CPPLIBS).qmake can also include other project file (fragments). For example, the project file in Example 7.1 includes a .pri file located in a directory relative to your environment variable CPPLIBS.

Example 7.1. src/qapp-gui/qapp-gui.pro

include ($$(CPPLIBS)/utils/common.pri) TEMPLATE = app # Input HEADERS += messager.h SOURCES += main.cpp messager.cpp

The command

qmake -project

produces a project file that contains information based on the contents of the current working directory. In particular, qmake cannot know about external libraries that you may need to build your project. So, if your project depends on an external library, you must edit the project file and add assignments to three of the variables.

For example, suppose we are developing an application that uses our utils library. The header files are located in $CPPLIBS/utils and the lib shared object files are located in $CPPLIBS. Then we must add the following lines to the project file

INCLUDEPATH += $$(CPPLIBS)/utils # the source header files LIBS += -L$$(CPPLIBS) # add this to the lib search path LIBS += -lutils # link with libutils.so

Assignments to the LIBS variable generally contain two kinds of linker switches that are passed directly to the compiler and the linker. For more information about what the linker switches mean see Section C.2.

Категории