E.1.  C++/Qt Setup: Open Source Platforms

[ fromfile: setup-nix.xml id: setup-nix ]

Open source development tools (ssh, bash, gcc) are available natively on UNIX and their related platforms. This includes Mac OS/X, which is a BSD-based distribution. [84]

When we discuss something that's specific to UNIX-derived platforms (Linux, BSD, Solaris, etc), we will use the shorthand *nix for “most flavors of UNIX.“

Another important acronym is POSIX, which stands for Portable Operating System Interface for UNIX. The development of this family of application programming interface (API) standards was sponsored by the IEEE (Institute of Electrical and Electronics Engineers), an organization for engineers, scientists, and students, best known for developing standards for the computer and electronics industry. [85]

This section is for readers who are using a computer with some flavor of *nix installed.

The examples in this book were tested with Qt 4.4. We recommend that you use the same version or a later one. The first step in setting up your computer for this book is to make sure that the full installation of Qt 4 is available to you. This includes, in addition to the source and compiled library code, the Qt Assistant documentation system, program examples, and the Qt Designer program.

[Note]Note

If your system has KDE 3.x (the K Desktop Environment) installed, then there is already a runtime version of Qt 3.x installed. Qt 4 needs to be installed separately. Our examples do not compile under Qt 3.

To see which (if any) version of Qt has already been installed on your system, start with the commands:

which qmake
qmake -v

The output of the first command tells you where the qmake executable is located. If that output looks like this: bash: qmake: command not found, it is possible that:

  1. The “full” Qt (including development tools) is not installed.

  2. It is installed, but your PATH does not include /path/to/qt4/bin

  3. It is installed by your package manager as qmake-qt4, to avoid conflict with same-named executables from Qt3.

If you can run it, qmake -v provides version information. If it reports Using Qt version 4.x.y, check whether these other Qt tools are available: moc, uic, assistant, and designer.

If these executables are all found and match versions, Qt 4 is installed and ready to use.

If the tests outlined above indicate that you have an earlier version or no Qt installed, or that you are missing some components of Qt 4, then you will need to build or install the latest release of Qt 4, and select the qt4 executables as defaults in your path.

Downloading from Source

You can download, unpack, and compile the latest open source tarball from Trolltech. Two typical places to unpack the tarball are /usr/local/ (if you are root) or $HOME/qt (if you are not).

[Tip] Installing dependencies

In Debian, it is possible with a single command to automatically install all of the tools and libraries you need to build another Debian package. You can take advantage of this, when you want to build any popular open-source tool from source. For more information, see Appendix D.

apt-get build-dep libqt4-dev
[Note]Note

A tarball is a file produced by the tar command (tape archive) that can combine many files, as specified in the command line, into one file (which is generally given the extension .tar) for ease of storage or transfer. The combined file is generally compressed using a utility like gzip or bzip2, which appends the extension .gz or .bz to the tar file.

The command line for unpacking a tarball depends on how it was created. You can usually determine this from its extension.

tar -vxf  whatever.tar               // uses the "verbose" switch
tar -zxf  whatever.tar.gz            // compressed with gzip
tar -zxf  whatever.tgz               // also compressed with gzip
tar -jxf  whatever.tar.bz2           // compressed with bzip2

A tar file can preserve directory structures and has many options and switches. You can read the online documentation for these utilities by typing:

 
info tar 
info gzip
info bzip

The Qt source tarball contains the complete source code of the Qt library, plus numerous examples, tutorials, and extensions with full reference documentation. The tarball contains simple installation instructions (in the README and INSTALL files) and a configure --help message. Be sure to read the README file before you attempt to install software from any open source tarball.

Compiling from source can take 2 to 3 hours (depending on the speed of your system), but it is worth the time. Example E.1 shows the options we use to configure Qt 4.

Example E.1. ../bin/qtconfigure

#!/bin/sh
# specify -phonon if you want to build the mp3player exercise or any
of the Phonon examples.
# See README.txt in libs/mp3player for details.

./configure -phonon -system-sqlite -verbose

# make

After Qt is configured, make and then install it.

[Tip]Tip

If you have a multi-core processor, try supplying a -j 4 to make, and it will run 4 compile processes simultaneously, taking advantage of your extra cores.

In the final step, make install copies the executables and headers into another location from the unzipped tarball source tree. If you are installing in a common location, you need to be root to do this.

Checking Qt's installation

[Tip]Tip

After installation, try qmake -v to determine which version of qmake is found by your shell. For systems that have more than one version installed, this is especially important to do.

[ezust@stan] /home/ezust> which qmake
/usr/local/Trolltech/Qt-4.4.1/bin/qmake
[ezust@stan] /home/ezust> qmake -version
QMake version 2.01a
Using Qt version 4.4.1 in /usr/local/Trolltech/Qt-4.4.1/lib

Environment Variables

After installing, check your environment variables and make sure that your PATH and LD_LIBRARY_PATH contain proper references to the built installed Qt.

[Tip]Tip

The bash command env will display the current values of all your environment variables.

Example E.2 shows how we set the values with bash, but the actual values depend on where the files are located on your system.

Example E.2. src/libs/utils/qt.sh

# Typical user specific environment and startup values for QT
# depending on how and where qt was installed on your system
export QTDIR=/usr/local/Trolltech/Qt-4.4.1
export PATH=$QTDIR/bin:$PATH
export MANPATH=$QTDIR/doc/man:$MANPATH
# Location of your own libraries - source and target.
export CPPLIBS=~/cs331/projects/libs

# LD Library path - where to search for shared object libraries
# at runtime.
export LD_LIBRARY_PATH=$CPPLIBS:$QTDIR/lib:$LD_LIBRARY_PATH




[84] BSD stands for Berkeley Software Distribution, a facility of the Computer Systems Research Group (CSRG) of the University of California at Berkeley. CSRG has been an important center of development for UNIX and for various basic protocols (e.g., TCP/IP) since the 1980s.

[85] If we wanted to write a POSIX regular expression (Section 14.3), for *nix, it might look like this: (lin|mac-os|un|solar|ir|ultr|ai|hp)[iu]?[sx].

[86] Qt 4 does not require the use of this environment variable but we use it to refer to the “Qt install” directory in our examples.