Using pkg-config with qmake

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Using pkg-config with qmake[edit | edit source]

Preface: Who has to read this page?[edit | edit source]

People who want to add external libs to their Qt applications.

What's pkg-config[edit | edit source]

pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line. So, for instance, an application can use

gcc -o test test.c $(pkg-config --libs --cflags glib-2.0)

rather than hard-coding the location of libs and include files. Manual page is to be found at pkg-config.

How pkg-config works[edit | edit source]

This utility takes data from .pc files in the /usr/lib/pkgconfig/ directory. Those files are installed by any library development package (eg: libsqlite3-dev).

 [sbox-FREMANTLE_ARMEL: ~] > dpkg -L libsqlite3-dev
 /.
 /usr
 /usr/include
 /usr/include/sqlite3.h
 /usr/include/sqlite3ext.h
 /usr/share
 /usr/share/doc
 /usr/share/doc/libsqlite3-dev
 /usr/share/doc/libsqlite3-dev/changelog.Debian.gz
 /usr/share/doc/libsqlite3-dev/README
 /usr/share/doc/libsqlite3-dev/copyright
 /usr/lib
 /usr/lib/libsqlite3.a
 /usr/lib/pkgconfig
 /usr/lib/pkgconfig/sqlite3.pc
 /usr/lib/libsqlite3.so

Which libs are managed by pkg-config[edit | edit source]

Usually every linux/Maemo library is managed by pkg-config and it appears in the list of managed packages:

 pkg-config --list-all

How to use pkg-config with qmake[edit | edit source]

Qt Project files (.pro) contain several variables used by the compiler, such as LIBS and INCLUDE. These variables contain the name and the location of the library required by the linker and the location of the include files used by the compiler.

When qmake creates a Makefile, it takes into account the predefined value of LIBS and INCLUDE coming from a Qt mkspec. The mkspec files are platform specific and they are stored in /usr/share/qt4/mkspecs/platform_name.

Anyway, Mkspecs files just keep Qt libs data. Therefore, a developer who wants to add new libraries has to tell to the compiler where the new libs are located.

This can be easily done by adding 2 lines to the project file:

unix {
    CONFIG += link_pkgconfig
    PKGCONFIG += <pc_file_without_extension>
}