pkg-config用于返回系统中已安装库的元信息。pkg-config对库元信息查询是通过在指定目录中去查询xxx.pc文件,从xxx.pc文件中解析得到库对元信息。
xxx:~$ pkg-config --variable pc_path pkg-config
/usr/lib64/pkgconfig:/usr/share/pkgconfig
xxx:~$
xxx:~$ echo $PKG_CONFIG_PATH
xxx:~$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
xxx:~$ echo $PKG_CONFIG_PATH
/usr/local/lib/pkgconfig
xxx:~$ export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:${PKG_CONFIG_PATH}
xxx:~$ echo $PKG_CONFIG_PATH
/usr/local/lib64/pkgconfig:/usr/local/lib/pkgconfig
pkg-config帮助信息
xxx:~$ pkg-config -h
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 if --cflags or --libs given on the command line
--silence-errors be silent about errors (default unless --cflags or --libsgiven 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
xxx:~$
查看所有的库
xxx:~$ pkg-config --list-all
sqlite3 SQLite - SQL database engine
xcb-xevie XCB Xevie - XCB Xevie Extension
xcb-damage XCB Damage - XCB Damage Extension
blkid blkid - Block device id library
pixman-1 Pixman - The pixman library (version 1)
......
libedata-book-1.2 libedatabook - Backend library for evolution address books
renderproto RenderProto - Render extension headers
xxx:~$
下面我们以libssl为例讲解其部分选项的使用方式。
libssl.pc内容
xxx:~$ cat /usr/lib64/pkgconfig/libssl.pc
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib64
includedir=${prefix}/include
Name: OpenSSL
Description: Secure Sockets Layer and cryptography libraries
Version: 1.0.1e
Requires:
Libs: -L${libdir} -lssl -lcrypto
Libs.private: -Wl,-z,relro -ldl -lz -L/usr/lib -lgssapi_krb5 -lkrb5 -lcom_err -lk5crypto
Cflags: -I${includedir} -I/usr/include
xxx:~$
[xxx:~$ pkg-config --modversion libssl
1.0.1v-dev
xxx:~$
xxx:~$ pkg-config --libs-only-l libssl
-lssl -lcrypto
xxx:~$
xxx:~$ pkg-config --libs-only-L libssl
xxx:~$
xxx:~$ pkg-config --exists libssl
xxx:~$ echo $?
0
xxx:~$ pkg-config --exists libssl2
xxx:~$ echo $?
1
xxx:~$
libssl是存在的,所以执行后的返回错误码为0;libssl2是不存在的,所以执行后返回错误码为1。
在configure文件中的应用
xxx:~$ pkg-config --exists --print-errors libssl
xxx:~$ echo $?
0
xxx:~$ pkg-config --exists --print-errors libssl2
Package libssl2 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libssl2.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libssl2' found
xxx:~$ echo $?
1
xxx:~$
想要自定义配置一个xxx.pc文件,需要先弄明白都有那些配置项可以配置,以及配置格式。
特别说明:pkg-config管理的文件必须是以“*.pc”的后缀名结尾。
配置格式形如:
xxx:~$ cat /usr/lib64/pkgconfig/libssl.pc
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib64
includedir=${prefix}/include
Name: OpenSSL
Description: Secure Sockets Layer and cryptography libraries
Version: 1.0.1e
Requires:
Libs: -L${libdir} -lssl -lcrypto
Libs.private: -Wl,-z,relro -ldl -lz -L/usr/lib -lgssapi_krb5 -lkrb5 -lcom_err -lk5crypto
Cflags: -I${includedir} -I/usr/include
xxx:~$
其实必须写的只有5个。Name、Description、Version、Cflags、Libs。