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 −

Option Description
--help Display help message
--about Show version and license information
--version Print supported pkg-config version
--atleast-pkgconfig-version Check compatibility with a specific pkg-config version
--errors-to-stdout Print errors to stdout instead of stderr
--print-errors Ensure all errors are displayed
--short-errors Show less verbose error messages
--silence-errors Suppress error messages
--list-all List all available packages
--list-package-names List package names only
--simulate Simulate dependency graph calculation
--no-cache Disable caching of seen packages
--log-file=filename Write log output to the specified file
--with-path=path Add a directory to the search path
--define-prefix Override prefix variable based on .pc file location
--dont-define-prefix Do not override prefix variable
--prefix-variable=varname Set package prefix variable name
--relocate=path Relocate a path and exit
--dont-relocate-paths Disable path relocation support
--personality=triplet|filename Set personality using a triplet or a file
--dump-personality Show personality details
--atleast-version Require a minimum version of a package
--exact-version Require an exact package version
--max-version Require a maximum package version
--exists Check if a package exists
--uninstalled Check if an uninstalled package will be used
--no-uninstalled Prevent usage of uninstalled packages
--no-provides Disable provides rules for dependencies
--maximum-traverse-depth Set max dependency graph depth
--static Compute dependencies for static linking
--shared Use a simplified dependency graph
--pure Optimize static dependency graphs
--env-only Use only PKG_CONFIG_PATH for package lookups
--ignore-conflicts Ignore conflict rules in modules
--validate Validate .pc file correctness
--define-variable=var=value Define a custom variable
--variable=varname Print a specific variable
--cflags Show required compiler flags
--cflags-only-I Show include path flags
--cflags-only-other Show non-include path flags
--libs Show required linker flags
--libs-only-L Show library path linker flags
--libs-only-l Show library name linker flags
--libs-only-other Show other linker flags
--print-requires Show required dependencies
--print-requires-private Show static linking dependencies
--print-provides Show provided dependencies
--print-variables List all known variables
--digraph Display dependency graph in Graphviz format
--keep-system-cflags Retain system -I/usr/include flags
--keep-system-libs Retain system library paths in the output
--path Show file paths of .pc files
--modversion Print package version
--internal-cflags Do not filter internal CFLAGS
--msvc-syntax Format output for MSVC compatibility
--fragment-filter=types Filter 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.

Advertisements