pkg-config is computer software that provides a unified interface for querying installed libraries for the purpose of compiling software from its source code. pkg-config was originally designed for Linux but is now also available for the various BSDs, Microsoft Windows, Mac OS X, and Solaris.
It outputs various information about installed libraries. This information may include:
Parameters for C or C++ compiler
Parameters for linker
Version of the package in question
The first implementation was written in shell. Later, it was rewritten in C using the GLib library.
语法
Synopsis
When a library is installed (automatically through the use of an RPM, deb, or other binary packaging system or by compiling from the source), a .pc file should be included and placed into a directory with other .pc files (the exact directory is dependent upon your system and outlined in the pkg-config man page). This file has several entries.
These entries typically contain a list of dependent libraries that programs using the package also need to compile. Entries also typically include the location of header files, version information and a description.
pc配置文件格式.
Here is an example .pc file for libpng:
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${exec_prefix}/include
Name: libpng
Description: Loads and saves PNG files
Version: 1.2.8
Libs: -L${libdir} -lpng12 -lz
Cflags: -I${includedir}/libpng12
This file demonstrates how libpng informs that its libraries can be found in /usr/local/lib and its headers in /usr/local/include, that the library name is libpng, and that the version is 1.2.8. It also gives the additional linker flags that are needed to compile code that uses this library.
编译时使用pkg-config提取libpng对应的依赖库和头文件目录.
Here is an example of usage of pkg-config while compiling:
$ gcc -o test test.c $(pkg-config --libs --cflags libpng)
PostgreSQL中取一个libpq.pc的例子 :
[root@150 pkgconfig]# pwd
/opt/pgsql/lib/pkgconfig
[root@150 pkgconfig]# ll
total 16
-rw-r--r--. 1 root root 273 Aug 5 11:24 libecpg_compat.pc
-rw-r--r--. 1 root root 245 Aug 5 11:24 libecpg.pc
-rw-r--r--. 1 root root 228 Aug 5 11:24 libpgtypes.pc
-rw-r--r--. 1 root root 251 Aug 5 11:24 libpq.pc
[root@150 pkgconfig]# cat libpq.pc
Name: libpq
Description: PostgreSQL libpq library
Url: http://www.postgresql.org/
Version: 9.3.5
Requires:
Requires.private:
Cflags: -I/opt/pgsql9.3.5/include
Libs: -L/opt/pgsql9.3.5/lib -lpq
Libs.private: -lssl -lcrypto -lcrypt -lldap_r -lpthread
[参考]
1. man pkg-config
2. http://en.wikipedia.org/wiki/Pkg-config