Linux Commands

Understanding the pkg-config Linux Command

“The pkg-config is a handy tool for compiling libraries and applications, especially for developers. It acts as a helper to ensure the correct compiler options get inserted on the command line with ease instead of hard-coding values, such as where to locate different libraries.

The pkg-config is free software that supports different platforms, including Unix-like systems, Windows, Linux, and Mac OS X. To use pkg-config, you only need glib installed or a working C library and compiler. Let’s look at the common usage and understand pkg-config more in the sections below.”

Installing pkg-config

You can install pkg-config from the apt database:

First, update the apt database.

$ sudo apt-get update

Next, install pkg-config.

$ sudo apt-get install -y pkg-config

pkg-config Description

pkg-config is a helper tool that mainly collects the metadata about installed libraries on computer systems and provides them to a user for easy assembling and integration. Each system has different libraries installed and to compile and link the libraries requires using pkg-config.

Furthermore, installing pkg-config alongside different packages makes it easy to adopt APIs in the case of developers. To be precise, pkg-config works by providing details necessary to compile and link programs to different libraries. Therefore, it fetches the metadata about the libraries instead of your hard coding on the terminal.

pkg-config Usage Examples

The metadata files that pkg-config retrieves information from have a .pc extension, and the name specified in the command is the metadata file.

1. Print the Link Flags

The –libs option is specified to display the link flags associated with a given package. In our example, we will use the OpenCV C++ library.

$ pkg-config opencv4 --libs

The displayed output link flags are:

2. Print Compile Flags

The –cflags prints the compile flags and the associated pre-processor required to compile a package plus the flags for its dependencies.

$ pkg-config opencv4 --cflags

3. Get a Version of the Library

You can check the version of a library using the —modversion flag.

$ pkg-config opencv4 --modversion

4. Print Errors

You can print an error message, such as a missing dependency or modules, using the command:

$ pkg-config opencv4 --print-errors

To print errors in the stdout instead of the default stderr, use the —errors-to-stdout option instead of the —print-errors.

5. Display Variables in a Package

To see all the variables included in a given package, use the —print-variables option:

$ pkg-config –print-variables opencv4

It will output all the names of any variable contained in your package.

6. List Packages

To show all the packages that have a .pc extension on your system, use the –list-all option

$ pkg-config –list-all

The output contains all packages listed in the PKG_CONFIG_PATH variable path.

7. Get Variable Value

You can get the value of a variable declared in a package’s .pc file. Use the —variable=[variable-name] [name-of-package] as in the example below.

$ pkg-config --variable=libdir opencv4

8. Define Variable

You can set a global variable value for a .pc file with pkg-config. For instance, to declare a variable named prefix in our OpenCV library, we can use the command:

$ pkg-config --define-variable=prefix=/sample --variable=prefix opencv4

9. Other Options

–help: it opens the help message for the pkg-config.

–debug: it shows the debug information.

–static: it displays libraries with static linking, including private libraries.

Format of pkg-config Files

If you have a module, in our case named sample, the module will have a .pc file that contains its metadata.

There is a specific format used with the modules. Furthermore, a module has particular keywords, including:

Name: it represents the name of the given library or package. In our case, it is a sample.

Description: it represents a description of the library or package.

Version: libraries have different versions, and the version is specified using this keyword.

URL: the URL links to an external place to download the library or get more details about its usage.

Requires: if the package requires other packages, they get specified here.

Libs: the link flags for the package

Cflags: the compiler flags associated with the package.

Conflicts: it highlights any packages that are likely to conflict with it.

Requires.private represents the required private packages not available to other applications.


The pkg-config Linux command offers many usage cases, and you can get more details about the tool from its manpage. We’ve covered most of its common usage cases to help you comfortably extract details of libraries and packages. That’s it, folks!

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.