How to use the command 'pkg-config' (with examples)

How to use the command 'pkg-config' (with examples)

pkg-config is a tool that provides the details of installed libraries for compiling applications. It helps developers by providing the necessary flags and dependencies to compile their code with specific libraries. This article illustrates various use cases of the ‘pkg-config’ command.

Use case 1: Get the list of libraries and their dependencies

Code:

pkg-config --libs library1 library2 ...

Motivation: When developing an application, it is essential to know the libraries required and their dependencies. Using the ‘pkg-config –libs’ command allows us to obtain this information easily. It is particularly useful for developers who need to ensure that their code has the correct libraries linked during compilation.

Explanation:

  • --libs is an option that instructs pkg-config to display the list of libraries and their associated linker flags.
  • library1, library2 and so on represent the names of the libraries for which we want to retrieve the information.

Example output:

-lgtk-3 -lwebkit2gtk-4.0

Use case 2: Get the list of libraries, their dependencies, and proper cflags for gcc

Code:

pkg-config --cflags --libs library1 library2 ...

Motivation: Sometimes, compiling code requires not only the libraries but also specific compiler flags, known as cflags. By using the ‘–cflags’ option, pkg-config will provide the necessary flags for gcc when compiling the code.

Explanation:

  • --cflags is an option that instructs pkg-config to display the C compiler flags needed to compile against the specified libraries.
  • --libs is the same as in use case 1, which provides the list of libraries and their associated linker flags.
  • library1, library2 and so on represent the names of the libraries for which we want to retrieve the information.

Example output:

-pthread -I/usr/include/gtk-3.0 -I/usr/include/webkit2gtk-4.0 -lgtk-3 -lwebkit2gtk-4.0

Use case 3: Compile your code with libgtk-3, libwebkit2gtk-4.0 and all their dependencies

Code:

c++ example.cpp $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0) -o example

Motivation: This use case demonstrates how to compile code with specific libraries and their associated dependencies. By using the ‘pkg-config –cflags –libs’ command within the compilation command, the necessary compiler flags and linker flags will be included automatically.

Explanation:

  • c++ example.cpp is the command to compile the ’example.cpp’ source code file.
  • $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0) is a subshell command that calls ‘pkg-config’ to provide the necessary compiler and linker flags for the specified libraries.
  • -o example specifies the output filename for the compiled executable as ’example’.

Example output: No output is shown if the compilation is successful.

Conclusion:

The ‘pkg-config’ command is a useful tool for developers who require information about installed libraries and their dependencies. By using various command options such as ‘–libs’ and ‘–cflags’, developers can easily retrieve the necessary flags and dependencies for compiling their code. Additionally, combining ‘pkg-config’ with compilation commands allows for seamless integration of libraries into the build process.

Related Posts

How to use the command 'pt' (with examples)

How to use the command 'pt' (with examples)

Platinum Searcher is a code search tool similar to the ‘ag’ command.

Read More
Using ldd to Display Shared Library Dependencies (with examples)

Using ldd to Display Shared Library Dependencies (with examples)

The ldd command is a useful tool for displaying the shared library dependencies of a binary.

Read More
Using the `pio ci` Command for Building PlatformIO Projects (with examples)

Using the `pio ci` Command for Building PlatformIO Projects (with examples)

1: Build a PlatformIO project in the default system temporary directory and delete it afterwards pio ci path/to/project Motivation: When working with PlatformIO projects, it is sometimes necessary to clone or copy an existing project to a different location for testing or remote build purposes.

Read More