文章目录
why
hard to integrate libraries
—> .pc file provide meta data
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: foo
Description: The foo library
Version: 1.0.0
Cflags: -I${includedir}/foo
Libs: -L${libdir} -lfoo
use .pc
Assuming that there are .pc files installed on the system,
the pkg-config
tool is used to extract the metadata for usage.
$ pkg-config --help
$ pkg-config --modversion foo
$ pkg-config --libs foo
$ pkg-config --libs --static bar
$ pkg-config --cflags bar
$ pkg-config --exists foo
$ pkg-config --libs "bar >= 2.7"
$ pkg-config --exists --print-errors xoxo
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
autoconf macro
PKG_PROG_PKG_CONFIG([MIN-VERSION]):
Locates the pkg-config tool on the system and checks the version for compatibility.
PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]):
Checks to see whether a particular set of modules exists.
PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]):
Checks to see whether a particular set of modules exists. If so, it sets <VARIABLE-PREFIX>_CFLAGS and <VARIABLE-PREFIX>_LIBS according to the output from pkg-config --cflags and pkg-config --libs.
comon usage pattern
common usage pattern: gcc
cc `pkg-config --cflags --libs x` -o myapp myapp.c
common usage pattern: autoconf & automake
configure.ac:
PKG_CHECK_MODULES([X], [x])
Makefile.am:
myapp_CFLAGS = $(X_CFLAGS)
myapp_LDADD = $(X_LIBS)
common usage pattern: cmake
pkg_check_modules(<PREFIX> [REQUIRED] [QUIET] <MODULE> [<MODULE>]*)
checks for all the given modules
pkg_search_module(<PREFIX> [REQUIRED] [QUIET] <MODULE> [<MODULE>]*)
checks for given modules and uses the first working one
It sets the following variables:
PKG_CONFIG_FOUND ... if pkg-config executable was found
PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
(since CMake 2.8.8)
<XPREFIX>_FOUND ... set to 1 if module(s) exist
<XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
<XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
<XPREFIX>_LDFLAGS ... all required linker flags
<XPREFIX>_LDFLAGS_OTHER ... all other linker flags
<XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
<XPREFIX>_CFLAGS ... all required cflags
<XPREFIX>_CFLAGS_OTHER ... the other compiler flags
<XPREFIX> = <PREFIX> for common case
<XPREFIX> = <PREFIX>_STATIC for static linking
<XPREFIX>_VERSION ... version of the module
<XPREFIX>_PREFIX ... prefix-directory of the module
<XPREFIX>_INCLUDEDIR ... include-dir of the module
<XPREFIX>_LIBDIR ... lib-dir of the module
<XPREFIX> = <PREFIX> when |MODULES| == 1, else
<XPREFIX> = <PREFIX>_<MODNAME>
pkg_check_modules (GLIB2 glib-2.0>=2.10)
requires at least version 2.10 of glib2 and defines e.g.
GLIB2_VERSION=2.10.3
pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
requires both glib2 and gtk2, and defines e.g.
FOO_glib-2.0_VERSION=2.10.3
FOO_gtk+-2.0_VERSION=2.8.20
refs
https://people.freedesktop.org/~dbn/pkg-config-guide.html
https://cmake.org/cmake/help/v3.0/module/FindPkgConfig.html