# pkg-config: Finding Packages on Your System Easily ## Introduction If a package is installed with `pkg-config` configuation file (\*.pc), then the package informations can be easily found. The informations includes package version, compile flags, link flags, etc. The `pkg-config` is convenient if a user wants to check whether a package is supported in a system, or needs compile options when building a project. ## The `pkg-config` Command Here are some examples of `pkg-config` command: * Print package version ``` $ pkg-config --version expat 0.29.2 ``` * Print link flags ``` $ pkg-config --libs expat -lexpat ``` * Print variable \*.pc files can define variables. Most packages define variables like `prefix`, `libdir`. ``` $ pkg-config --variable=prefix expat /usr $ pkg-config --variable=libdif expat /usr/lib64 ``` ## Building a Project with `pkg-config` To build a project base on the output of `pkg-config`, one can use command substitution to get the compile flags or link flags. This is more flexible than hardcoding flags in build scripts like makefile. For example: ``` $ gcc hello.c -o hello $(pkg-config --cflags --libs ncurses) ``` The command expands to: ``` $ gcc hello.c -o hello -D_GNU_SOURCE -D_DEFAULT_SOURCE -lncurses -ltinfo ``` ## A simple .pc File Here is a simple .pc file in expat library: ``` $ cat expat.pc prefix=/usr exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir=${prefix}/include Name: expat Version: 2.2.0 Description: expat XML parser URL: http://www.libexpat.org Libs: -L${libdir} -lexpat Cflags: -I${includedir} ``` The .pc file format is simple that I think there is no need to have more explanation :slightly_smiling_face:. ## References [1] [Guide to pkg-config (by Dan Nicholson)](https://people.freedesktop.org/~dbn/pkg-config-guide.html) [2] [pkg-config man page](https://linux.die.net/man/1/pkg-config)