Qt 4.0: Configuring qmake's Environment

|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

[Previous: qmake Function Reference] [Contents] [Next: qmake Advanced Usage]

# Configuring qmake's Environment

- Properties
- QMAKESPEC
- INSTALLS
- Cache File
- Library Dependencies
- File Extensions
- Customizing Makefile Output

## Properties

`qmake` has a system of persistant information, this allows you to `set` a variable in qmake once, and each time qmake is invoked this value can be queried. Use the following to set a property in qmake:

        qmake -set VARIABLE VALUE

To retrieve this information back from qmake you can do:

        qmake -query VARIABLE
        qmake -query #queries all current VARIABLE/VALUE pairs..

This information will be saved into a QSettings object (meaning it will be stored in different places for different platforms). As VARIABLE is versioned as well, you can set one value in an older version of qmake, and newer versions will retrieve this value, however if you -set VARIABLE into a newer version of qmake the older version will not use this value. You can however query a specific version of a variable if you prefix that version of qmake to VARIABLE, as in:

        qmake -query "1.06a/VARIABLE"

qmake also has the notion of `builtin` properties, for example you can query the installation of Qt for this version of qmake with the QT_INSTALL_PREFIX property:

        qmake -query "QT_INSTALL_PREFIX"

These builtin properties cannot have a version prefixed to them as they are not versioned and each qmake will have its own notion of these values. The list below outlines the builtin properties:

- QT_INSTALL_PREFIX - Where the version of Qt this qmake is built for resides
- QT_INSTALL_DATA - Where data for this version of Qt resides
- QMAKE_VERSION - The current version of qmake

Finally, these values can be queried in a project file with a special notation such as:

        QMAKE_VERS = $$[QMAKE_VERSION]

## QMAKESPEC

`qmake` requires a platform and compiler description file which contains many default values used to generate appropriate Makefiles. The standard Qt distribution comes with many of these files, located in the `mkspecs` subdirectory of the Qt installation.

The `QMAKESPEC` environment variable can contain any of the following:

- A complete path to a directory containing a qmake.conf file. In this case `qmake` will open the qmake.conf file from within that directory. If the file does not exist, `qmake` will exit with an error.
- The name of a platform-compiler combination. In this case, `qmake` will search in the directory specified by the `mkspecs` subdirectory of the data path specified when Qt was compiled (see QLibraryInfo::DataPath).

Note: the QMAKESPEC path will automatically be added to the INCLUDEPATH system variable.

## INSTALLS

It is common on Unix to be able to install from the same utility as you build with (e.g make install). For this `qmake` has introduce the concept of an install set. The notation for this is quite simple, first you fill in an "object" in qmake for example:

        documentation.path = /usr/local/program/doc
        documentation.files = docs/*

In this way you are telling `qmake` several things about this install, first that you plan to install to /usr/local/program/doc (the path member), second that you plan to copy everything in the docs directory. Once this is done you may insert it in the install list:

        INSTALLS += documentation

Now `qmake` will take over making sure the correct things are copied to the specified places. If however you require greater control you may use the `extra` member of the object:

        unix:documentation.extra = create_docs; mv master.doc toc.doc

Then qmake will run the things in extra (this is of course platform specific, so you may need to test for your platform first, this case we test for unix). Then it will do the normal processings of the files member. Finally if you appened a builtin install to INSTALLS `qmake` (and do not specify a files or extra member) will decide what needs to be copied for you, currently the only supported builtin is target:

        target.path = /usr/local/myprogram
        INSTALLS += target

With this `qmake` will know what you plan need copied, and do this for you.

## Cache File

The cache file (mentioned above in the options) is a special file `qmake` will read to find settings not specified in the `qmake.conf` file, the .pro file, or the command line. If `-nocache` is not specified, `qmake` will try to find a file called `.qmake.cache` in parent directories. If it fails to find this file, it will silently ignore this step of processing.

## Library Dependencies

Often when linking against a library `qmake` relies on the underlying platform to know what other libraries this library links against, and lets the platform pull them in. In many cases, however, this is not sufficent. For example when statically linking a library there are no libraries linked against, and therefore no dependencies to those libraries are created - however an application that later links against this library will need to know where to find the symbols that the linked in library will require. To help with this situation `qmake` will follow a library's dependencies when it feels appropriate, however this behaviour must be enabled in `qmake`. To enable requires two steps. First, you must enable it in the library - to do this you must tell `qmake` to save information about this library:

        CONFIG += create_prl

This is only relevant to the lib template, and will be ignored for all others. When this option is enabled `qmake` will create a file (called a .prl file) which will save some meta information about the library. This metafile is itself just a qmake project file, but with all internal variables. You are free to view this file, and if deleted `qmake` will know to recreate it when necesary (either when the .pro file is later read, or if a dependent library (described below) has changed). When installing this library (by using target in INSTALLS, above) `qmake` will automatically copy the .prl file to your install path.

The second step to enabling this processing is to turn on reading of the meta information created above:

        CONFIG += link_prl

When this is turned on `qmake` will process all libraries linked to, and find their meta information. With this meta information `qmake` will figure out what is relevant to linking, specifically it will add to your list of DEFINES as well as LIBS. Once `qmake` has processed this file, it will then look through the newly introduced LIBS and find their dependent .prl files, and continue until all libraries have been resolved. At this point the Makefile is created as usual, and the libraries are linked explicity against your program.

The internals of the .prl file are left closed so they can easily change later. It is not designed to be changed by hand however, and should only be created by `qmake` - these .prl files should also not be transfered from operating system to operating system as they may be platform dependent (like a Makefile).

## File Extensions

Under normal circumstances `qmake` will try to use appropriate file extensions for your platform. There may be times, however, that you would like to override the behavior of these extensions. To do this, you must modify builtin variables in your .pro file, which will in turn changes `qmake`'s interpretation of these files. You may do this as:

        QMAKE_EXT_MOC = .mymoc

The variables are as follows:

- QMAKE_EXT_MOC - This modifies the extension placed on included moc files.
- QMAKE_EXT_UI - This modifies the extension used for designer UI files (usually in FORMS).
- QMAKE_EXT_PRL - This modifies the extension placed on library dependency files.
- QMAKE_EXT_LEX - This changes the suffix used in files (usually in LEXSOURCES).
- QMAKE_EXT_YACC - This changes the suffix used in files (usually in YACCSOURCES).
- QMAKE_EXT_OBJ - This changes the suffix used on generated object files.

All the above accept just the first value, so you must assign to it one value that will be used through your Makefile. There are two variables that accept a list of values, they are:

- QMAKE_EXT_CPP - Changes interpretation all files with these suffixes to be C++ source files.
- QMAKE_EXT_H - Changes interpretation all files with these suffixes to be C header files.

## Customizing Makefile Output

`qmake` often tries to be all things to all build tools, this is often less than ideal when you really need to run special platform dependent commands. This can be achieved with specific instructions to the different `qmake` backends.

The interfaces to customizing the Makefile are done through "objects" as in other places in qmake. The notation for this is quite simple, first you fill in an "object" in qmake for example:

        mytarget.target = .buildfile
        mytarget.commands = touch $$mytarget.target
        mytarget.depends = mytarget2

        mytarget2.commands = @echo Building $$mytarget.target

The information above defines a qmake target called mytarget which contains a Makefile target called .buildfile, .buildfile is generated by 'touch .buildfile', and finally that this Makefile target depends on the qmake target mytarget2. Additionally we've defined the qmake target mytarget2 which simply echo's something to stdout.

The final step to making use of the above is to instruct qmake that this is actually an object used by the target building parts of qmake by:

        QMAKE_EXTRA_TARGETS += mytarget mytarget2

This is all you need to do to actually build custom targets in qmake, of course you may want to tie one of these targets to actually building the qmake build target. To do this, you simply need to include your Makefile target in the list of PRE_TARGETDEPS.

For convenience there is also a method of customizing projects for generic new compilers (or even preprocessors).

        new_moc.output  = moc_${QMAKE_FILE_BASE}.cpp
        new_moc.commands = moc ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
        new_moc.depend_command = g++ -E -M ${QMAKE_FILE_NAME} | sed "s,^.*: ,,"
        new_moc.input = NEW_HEADERS
        QMAKE_EXTRA_COMPILERS += new_moc

With this you can create a new moc for qmake, the commands will be executed over all arguments given to a `NEW_HEADERS` variable (from the input variable), and write to output (and automatically hand this filename to the compiler to be linked into your target). Additionally `qmake` will execute depend_command to generate dependency information and place this in the project as well.

These commands can easily be placed into a cache file, and subsequent .pro files can give several arguments to `NEW_HEADERS`.

[Previous: qmake Function Reference] [Contents] [Next: qmake Advanced Usage]