pkg-config是用于查找在系统中已安装的库的工具,它可以查找库的信息包括:
- 编译器所需要的参数–cflags
- 链接器所需要的参数–libs
- 软件包的版本信息等
在Ununtu中pkg-config的安装方法:
sudo apt-get install pkg-config
pkg-config会默认搜索 /usr/lib/pkgconfig和/usr/share/pkgconfig下的 .pc 配置文件,或者将搜索文件夹加入PKG_CONFIG_PATH环境变量,例如:
PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/[库名字]
下面是gtk±3.0 .pc文件内容的例子:
prefix=/usr
exec_prefix=${prefix}
libdir=/usr/lib/x86_64-linux-gnu
includedir=${prefix}/include
targets=x11 broadway wayland
gtk_binary_version=3.0.0
gtk_host=x86_64-pc-linux-gnu
Name: GTK+
Description: GTK+ Graphical UI Library
Version: 3.22.30
Requires: gdk-3.0 atk >= 2.15.1 cairo >= 1.14.0 cairo-gobject >= 1.14.0 gdk-pixbuf-2.0 >= 2.30.0 gio-2.0 >= 2.49.4
Requires.private: atk atk-bridge-2.0 wayland-client >= 1.9.91 wayland-protocols >= 1.12 xkbcommon >= 0.2.0 wayland-cursor >= 1.9.91 wayland-egl epoxy >= 1.0 pangoft2 gio-unix-2.0 >= 2.49.4
Libs: -L${libdir} -lgtk-3
Cflags: -I${includedir}/gtk-3.0
pkg-config的使用方法:
pkg-config --help
Usage:
pkg-config [OPTION?]
Help Options:
-h, --help Show help options
Application Options:
--version output version of pkg-config
--modversion output version for package
--atleast-pkgconfig-version=VERSION require given version of pkg-config
--libs output all linker flags
--static output linker flags for static linking
--short-errors print short errors
--libs-only-l output -l flags
--libs-only-other output other libs (e.g. -pthread)
--libs-only-L output -L flags
--cflags output all pre-processor and compiler flags
--cflags-only-I output -I flags
--cflags-only-other output cflags not covered by the cflags-only-I option
--variable=NAME get the value of variable named NAME
--define-variable=NAME=VALUE set variable NAME to VALUE
--exists return 0 if the module(s) exist
--print-variables output list of variables defined by the module
--uninstalled return 0 if the uninstalled version of one or more module(s) or their dependencies will be used
--atleast-version=VERSION return 0 if the module is at least version VERSION
--exact-version=VERSION return 0 if the module is at exactly version VERSION
--max-version=VERSION return 0 if the module is at no newer than version VERSION
--list-all list all known packages
--debug show verbose debug information
--print-errors show verbose information about missing or conflicting packages (default unless --exists or --atleast/exact/max-version given on the command line)
--silence-errors be silent about errors (default when --exists or --atleast/exact/max-version given on the command line)
--errors-to-stdout print errors from --print-errors to stdout not stderr
--print-provides print which packages the package provides
--print-requires print which packages the package requires
--print-requires-private print which packages the package requires for static linking
--validate validate a package's .pc file
--define-prefix try to override the value of prefix for each .pc file found with a guesstimated value based on the location of the .pc file
--dont-define-prefix don't try to override the value of prefix for each .pc file found with a guesstimated value based on the location of the .pc file
--prefix-variable=PREFIX set the name of the variable that pkg-config automatically sets
例如引入头文件:
# pkg-config --cflags-only-I [lib_name]
pkg-config --cflags-only-I gtk+-3.0
/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include