Understanding the pkg-config Linux Command

The “pkg-config” is a command line tool to aid the developers in including and linking simply by specifying the name and version. The tool searches for “*.pc” files in specific directories defined by an environment variable, PKG_CONFIG_PATH, including the name, version, or flags.

Developers can compile libraries and applications from the terminal without fussing over hard-coded values like the path to the libraries. 

This post will provide an insight into the functionality of the pkg-config command line utility in Linux with the following outcomes: 

  • How to Install pkg-config on Linux?
  • How to Use the pkg-config Command of Linux?

How to Install pkg-config on Linux?

The pkg-config is pre-installed on most of the Linux distros; if it isn’t, then use the appropriate commands as per your Linux distributions: 

$ sudo apt-get install pkg-config                 #For Ubuntu/Debian
$ sudo yum install pkg-config                     #For RedHat/Fedora
$ sudo pacman -S pkgconf                          #For Arch Linux

If any of the above commands aren’t working, we recommend using your system’s software/package manager (GUI method).

How to Use the pkg-config Command of Linux?

The basic syntax of the pkg-config command is mentioned below:

$ pkg-config [options] [library]

The “options” are the flags that are optional and used to specify the type of information that you want to see. In comparison, the “library” is the name whose data you want to see. Let’s shift the focus to the options/flags now.

The “pkg-config” command of Linux comes with various flags/options that you can use. Since we must work with the libraries, this command lists them all.

$ pkg-config --list-all

When you see the above image, there are two columns. On the left is the library’s name, while on the right is a description.

Example 1: Display the cflags of a Library

The C Compiler Flags (cflags) is an option of the “pkg-config” that is used to view the preprocessor and compiler flags to allow the packages to be compiled on the command line. Let’s find the necessary header files when building a Python program.

$ pkg-config --cflags python3

In the above image, two paths are mentioned, from which the first one is the path “-I/usr/include/python3.10,” which is the location of the Python 3.10 include files. While the second is the path “-I/usr/include/x86_64-linux-gnu/python3.10,” which is the location of the Python 3.10 files for x86_64-linux-gnu architecture.

Example 2: Print/View the Link Flags of a Library

To retrieve the link flags of a library, the “–libs” option is used. The link flags are command-line options passed to the linker building the program and can be used to specify library paths, libraries to link against, and other options that affect how the program is linked. For example, let’s retrieve the flags of the GTK+ library.

$ pkg-config --libs gtk+-3.0

The output of the above command is the linker flags. The flag -l is used to specify the library that the linker should link against. For example, -lgtk-3 tells the linker to link against the gtk-3 library.

Example 3: Check Version Details of a Library

To check the version-related details of a library using the “pkg-config” command, we have the “–modversion” option. Let’s check the version of the GTK+ library.

$ pkg-config --modversion  gtk+-3.0

In the above image, the output “3.24.33” is the version of the GTK+ library, and by using it, users can know which build of the software is compatible with which version of the library.

Example 4: Print/View Errors

The “–print-errors” option of the “pkg-config” command is used to view the library-related errors, like whether it is installed correctly and properly configured on the system. Let’s check if the errors are for the GTK+ library.

$ pkg-config --print-errors gtk+-3.0

If the output, like in the above image, is blank, then there’s no error. If there’s an error, it will print saying “library not found” or something similar.

Example 5: Display/View the variables in any Package

If you want to see all variables in the package, found in the “*.pc” file and many other details like version, included files, and libraries. Let’s see the variables of the GTK+ library.

$ pkg-config --print-variable gtk+-3.0

When executed, the above command displays the variables defined in the “*.pc” file of the package, and they specify the include & library paths and a bunch of other information. 

Note: These variables aren’t the same ones used in your code.

Example 6: View Debug Information

If you need to check the debug-related information, use the “–debug with the “pkg-config” command. This is a helpful feature for developers. Let’s check the debug information of the library named GTK+.

$ pkg-config --debug gtk+-3.0

As expected, the above command prints the specified package’s debug information.

Example 7: Display the Static Linking

Static linking can be defined as a method to link the libraries included in the program’s executable file allowing them (libraries) to be used without being installed on your system. This is used on the system where the necessary libraries aren’t installed. Let’s view the static linked libraries of the GTK+.

$ pkg-config --cflags --static gtk+-3.0

The output displayed in the above image represents the static linked libraries of GTK+ and includes the private libraries.

Conclusion

The pkg-config command of Linux is the all-in-one solution for working with libraries while building a package. As discussed above, it also shows tons of information related to libraries and much more, and this command line tool is beneficial for developers.