pkg-config Command in Linux


The pkg-config command in Linux displays the meta information of the installed libraries in the system. It helps manage, compile, and link flags needed to use those libraries in programs.

Table of Contents

Here is a comprehensive guide to the options available with the pkg-config command −

Installation of pkg-config Command in Linux

The pkg-config command may not be preinstalled on all Linux distributions. To install it on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command −

sudo apt install pkg-config

To install it on Arch Linux, use the command given below −

sudo pacman -S pkgconf-pkg-config

To install pkg-config on Fedora, use the following command −

sudo dnf install pkgconf-pkg-config

To verify the installation of the pkg-config command, check its version −

pkg-config --version
pkg-config Command in Linux1

Syntax of pkg-config Command

The syntax of the pkg-config command in Linux is as follows −

pkg-config [options] [library]

The [options] field is used to specify various options to retrieve specific information, and the [library] field is used to specify the name of the package.

pkg-config Command Options

The options of the Linux pkg-config are listed below −

OptionDescription
--helpDisplay help message
--aboutShow version and license information
--versionPrint supported pkg-config version
--atleast-pkgconfig-versionCheck compatibility with a specific pkg-config version
--errors-to-stdoutPrint errors to stdout instead of stderr
--print-errorsEnsure all errors are displayed
--short-errorsShow less verbose error messages
--silence-errorsSuppress error messages
--list-allList all available packages
--list-package-namesList package names only
--simulateSimulate dependency graph calculation
--no-cacheDisable caching of seen packages
--log-file=filenameWrite log output to the specified file
--with-path=pathAdd a directory to the search path
--define-prefixOverride prefix variable based on .pc file location
--dont-define-prefixDo not override prefix variable
--prefix-variable=varnameSet package prefix variable name
--relocate=pathRelocate a path and exit
--dont-relocate-pathsDisable path relocation support
--personality=triplet|filenameSet personality using a triplet or a file
--dump-personalityShow personality details
--atleast-versionRequire a minimum version of a package
--exact-versionRequire an exact package version
--max-versionRequire a maximum package version
--existsCheck if a package exists
--uninstalledCheck if an uninstalled package will be used
--no-uninstalledPrevent usage of uninstalled packages
--no-providesDisable provides rules for dependencies
--maximum-traverse-depthSet max dependency graph depth
--staticCompute dependencies for static linking
--sharedUse a simplified dependency graph
--pureOptimize static dependency graphs
--env-onlyUse only PKG_CONFIG_PATH for package lookups
--ignore-conflictsIgnore conflict rules in modules
--validateValidate .pc file correctness
--define-variable=var=valueDefine a custom variable
--variable=varnamePrint a specific variable
--cflagsShow required compiler flags
--cflags-only-IShow include path flags
--cflags-only-otherShow non-include path flags
--libsShow required linker flags
--libs-only-LShow library path linker flags
--libs-only-lShow library name linker flags
--libs-only-otherShow other linker flags
--print-requiresShow required dependencies
--print-requires-privateShow static linking dependencies
--print-providesShow provided dependencies
--print-variablesList all known variables
--digraphDisplay dependency graph in Graphviz format
--keep-system-cflagsRetain system -I/usr/include flags
--keep-system-libsRetain system library paths in the output
--pathShow file paths of .pc files
--modversionPrint package version
--internal-cflagsDo not filter internal CFLAGS
--msvc-syntaxFormat output for MSVC compatibility
--fragment-filter=typesFilter output by specified types

Examples of pkg-config Command in Linux

In this section, the usage of the pkg-config command in Linux will be discussed with examples −

  • Listing all the Installed Packages
  • Displaying Compiler Flags
  • Displaying Required Linker Flags
  • Checking if a Package Exists
  • Displaying the Package Version
  • Checking if the Installed Version meets the Specific Requirement
  • Displaying the .pc File Path of a Package
  • Displaying all Variables used in a Package
  • Displaying a Variable Value
  • Compiling a C Program using pkg-config Command
  • Displaying the Dependency Graph
  • Displaying Usage Help

Listing all the Installed Packages

To list all the installed packages, use the --list-all option with the pkg-config command −

pkg-config --list-all
pkg-config Command in Linux2

The --list-all option scans all directories in the PKG_CONFIG_PATH environment variable and lists all available packages with their details.

To list the package names without the description, use the --list-package-names option −

pkg-config --list-package-names
pkg-config Command in Linux3

Displaying Compiler Flags

To display the necessary compiler flags for a package, use the --cflags option −

pkg-config --cflags zlib

Displaying Required Linker Flags

To print the required linker flags, use the --libs option −

pkg-config --libs libssl
pkg-config Command in Linux4

Checking if a Package Exists

To verify whether a package exists or not, use the --exists option. For example, to check if zlib exists or not, use the following command −

pkg-config --exists zlib && echo "zlib is installed" || echo "zlib is not installed"
pkg-config Command in Linux5

Note that the --exists option does not display output; it only sets the exit code.

Displaying Package Version

To display the installed version of a package, use the --modversion option −

pkg-config --modversion zlib
pkg-config Command in Linux6

Checking if the Installed Version meets the Specific Requirement

To check if the installed version meets the specific version requirement, use the --atleast-version option −

pkg-config --atleast-version=2.60 zlib && echo "Version is sufficient" || echo "Version is not sufficient"
pkg-config Command in Linux7

The --atleast-version option checks whether the installed version is greater than or equal to the specified version.

Displaying the .pc File Path of a Package

To display the .pc file path of a package, use the --path option −

pkg-config --path zlib
pkg-config Command in Linux8

Displaying all Variables used in a Package

To list all available variables defined in a package's .pc file, use the --print-variables option with the pkg-config command −

pkg-config --print-variables zlib
pkg-config Command in Linux9

Displaying a Variable Value

To display the variable value, use the --variable option with the variable name. For example, to display the value of the libdir variable, use the pkg-config command in the following way −

pkg-config --variable=libdir zlib
pkg-config Command in Linux10

Compiling a C Program using pkg-config Command

To compile a C program that depends upon libssl, use the following command −

gcc code.c $(pkg-config --cflags --libs libssl) -o code

In the above command, the pkg-config fetches the necessary compiler and linker flags automatically. Instead of manually specifying include paths and libraries, pkg-config handles it dynamically.

Displaying the Dependency Graph

To display the dependency graph, use the --digraph option with the pkg-config command −

pkg-config --digraph zlib
pkg-config Command in Linux11

The above command displays a dependency graph in Graphviz format.

Displaying Usage Help

To display the usage help, use the --help option with the pkg-config command −

pkg-config --help

Conclusion

The pkg-config command in Linux is a useful tool for managing and retrieving metadata about installed libraries, making it easier to compile and link programs that depend on them. It provides various options to list packages, check dependencies, display compiler and linker flags, and verify installed versions.

The pkg-config command simplifies package management by automating the retrieval of required information, reducing the complexity of manual configuration.