FindPkgConfig¶
A pkg-config module for CMake.
Finds the pkg-config executable and adds the pkg_get_variable(),
pkg_check_modules() and pkg_search_module() commands. The
following variables will also be set:
PKG_CONFIG_FOUNDTrue if a pkg-config executable was found.
PKG_CONFIG_VERSION_STRING在 2.8.8 版被加入.
The version of pkg-config that was found.
PKG_CONFIG_EXECUTABLEThe pathname of the pkg-config program.
PKG_CONFIG_ARGN在 3.22 版被加入.
A list of arguments to pass to pkg-config.
Both PKG_CONFIG_EXECUTABLE and PKG_CONFIG_ARGN are initialized by the
module, but may be overridden by the user. See Variables Affecting Behavior
for how these variables are initialized.
- pkg_check_modules¶
Checks for all the given modules, setting a variety of result variables in the calling scope.
pkg_check_modules(<prefix> [REQUIRED] [QUIET] [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH] [IMPORTED_TARGET [GLOBAL]] <moduleSpec> [<moduleSpec>...])
When the
REQUIREDargument is given, the command will fail with an error if module(s) could not be found.When the
QUIETargument is given, no status messages will be printed.在 3.3 版被加入: The
CMAKE_PREFIX_PATH,CMAKE_FRAMEWORK_PATH, andCMAKE_APPBUNDLE_PATHcache and environment variables will be added to thepkg-configsearch path. TheNO_CMAKE_PATHandNO_CMAKE_ENVIRONMENT_PATHarguments disable this behavior for the cache variables and environment variables respectively. ThePKG_CONFIG_USE_CMAKE_PREFIX_PATHvariable set toFALSEdisables this behavior globally.在 3.7 版被加入: The
IMPORTED_TARGETargument will create an imported target namedPkgConfig::<prefix>that can be passed directly as an argument totarget_link_libraries().在 3.13 版被加入: The
GLOBALargument will make the imported target available in global scope.在 3.15 版被加入: Non-library linker options reported by
pkg-configare stored in theINTERFACE_LINK_OPTIONStarget property.在 3.18 版的變更: Include directories specified with
-isystemare stored in theINTERFACE_INCLUDE_DIRECTORIEStarget property. Previous versions of CMake left them in theINTERFACE_COMPILE_OPTIONSproperty.Each
<moduleSpec>can be either a bare module name or it can be a module name with a version constraint (operators=,<,>,<=and>=are supported). The following are examples for a module namedfoowith various constraints:foomatches any version.foo<2only matches versions before 2.foo>=3.1matches any version from 3.1 or later.foo=1.2.3requires that foo must be exactly version 1.2.3.
The following variables may be set upon return. Two sets of values exist: One for the common case (
<XXX> = <prefix>) and another for the informationpkg-configprovides when called with the--staticoption (<XXX> = <prefix>_STATIC).<XXX>_FOUNDset to 1 if module(s) exist
<XXX>_LIBRARIESonly the libraries (without the '-l')
<XXX>_LINK_LIBRARIESthe libraries and their absolute paths
<XXX>_LIBRARY_DIRSthe paths of the libraries (without the '-L')
<XXX>_LDFLAGSall required linker flags
<XXX>_LDFLAGS_OTHERall other linker flags
<XXX>_INCLUDE_DIRSthe '-I' preprocessor flags (without the '-I')
<XXX>_CFLAGSall required cflags
<XXX>_CFLAGS_OTHERthe other compiler flags
All but
<XXX>_FOUNDmay be a ;-list if the associated variable returned frompkg-confighas multiple values.在 3.18 版的變更: Include directories specified with
-isystemare stored in the<XXX>_INCLUDE_DIRSvariable. Previous versions of CMake left them in<XXX>_CFLAGS_OTHER.There are some special variables whose prefix depends on the number of
<moduleSpec>given. When there is only one<moduleSpec>,<YYY>will simply be<prefix>, but if two or more<moduleSpec>items are given,<YYY>will be<prefix>_<moduleName>.<YYY>_VERSIONversion of the module
<YYY>_PREFIXprefix directory of the module
<YYY>_INCLUDEDIRinclude directory of the module
<YYY>_LIBDIRlib directory of the module
在 3.8 版的變更: For any given
<prefix>,pkg_check_modules()can be called multiple times with different parameters. Previous versions of CMake cached and returned the first successful result.在 3.16 版的變更: If a full path to the found library can't be determined, but it's still visible to the linker, pass it through as
-l<name>. Previous versions of CMake failed in this case.Examples:
pkg_check_modules (GLIB2 glib-2.0)
Looks for any version of glib2. If found, the output variable
GLIB2_VERSIONwill hold the actual version found.pkg_check_modules (GLIB2 glib-2.0>=2.10)
Looks for at least version 2.10 of glib2. If found, the output variable
GLIB2_VERSIONwill hold the actual version found.pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
Looks for both glib2-2.0 (at least version 2.10) and any version of gtk2+-2.0. Only if both are found will
FOObe considered found. TheFOO_glib-2.0_VERSIONandFOO_gtk+-2.0_VERSIONvariables will be set to their respective found module versions.pkg_check_modules (XRENDER REQUIRED xrender)
Requires any version of
xrender. Example output variables set by a successful call:XRENDER_LIBRARIES=Xrender;X11 XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
- pkg_search_module¶
The behavior of this command is the same as
pkg_check_modules(), except that rather than checking for all the specified modules, it searches for just the first successful match.pkg_search_module(<prefix> [REQUIRED] [QUIET] [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH] [IMPORTED_TARGET [GLOBAL]] <moduleSpec> [<moduleSpec>...])
在 3.16 版被加入: If a module is found, the
<prefix>_MODULE_NAMEvariable will contain the name of the matching module. This variable can be used if you need to runpkg_get_variable().Example:
pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
- pkg_get_variable¶
在 3.4 版被加入.
Retrieves the value of a pkg-config variable
varNameand stores it in the result variableresultVarin the calling scope.pkg_get_variable(<resultVar> <moduleName> <varName> [DEFINE_VARIABLES <key>=<value>...])
If
pkg-configreturns multiple values for the specified variable,resultVarwill contain a ;-list.Options:
DEFINE_VARIABLES <key>=<value>...在 3.28 版被加入.
Specify key-value pairs to redefine variables affecting the variable retrieved with
pkg-config.
For example:
pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
Variables Affecting Behavior¶
- PKG_CONFIG_EXECUTABLE¶
This cache variable can be set to the path of the pkg-config executable.
find_program()is called internally by the module with this variable.在 3.1 版被加入: The
PKG_CONFIGenvironment variable can be used as a hint ifPKG_CONFIG_EXECUTABLEhas not yet been set.在 3.22 版的變更: If the
PKG_CONFIGenvironment variable is set, only the first argument is taken from it when using it as a hint.
- PKG_CONFIG_ARGN¶
在 3.22 版被加入.
This cache variable can be set to a list of arguments to additionally pass to pkg-config if needed. If not provided, it will be initialized from the
PKG_CONFIGenvironment variable, if set. The first argument in that environment variable is assumed to be the pkg-config program, while all remaining arguments after that are used to initializePKG_CONFIG_ARGN. If no such environment variable is defined,PKG_CONFIG_ARGNis initialized to an empty string. The module does not update the variable once it has been set in the cache.
- PKG_CONFIG_USE_CMAKE_PREFIX_PATH¶
在 3.1 版被加入.
Specifies whether
pkg_check_modules()andpkg_search_module()should add the paths in theCMAKE_PREFIX_PATH,CMAKE_FRAMEWORK_PATHandCMAKE_APPBUNDLE_PATHcache and environment variables to thepkg-configsearch path.If this variable is not set, this behavior is enabled by default if
CMAKE_MINIMUM_REQUIRED_VERSIONis 3.1 or later, disabled otherwise.