When a library defines a build with CMake and goes through the trouble of building an install package for itself, there will be a XXXConfig.cmake
.
If a library doesn't have a way to export it's targets to CMake, CMake tries to bridge the gap by providing FindXXX.cmake
scripts that attempt to locate such libraries.
In the docs, FindXXX.cmake
(module mode), is attempted first, and only if that fails does it attempt to use XXXConfig.cmake
(config mode). But this seems like a really backwards to me.
The problem is, for example, I have built CURL from source, and the ConfigXXX produces a different target name than FindXXX, so, when trying to use it, it fails because FindXXX took responsibility for the find_package request and loaded a different target name than what I was expecting.
Can I at least tell CMake somehow to do things the other way around? Config mode first.
I know I can disable module mode entirely, but I'd rather have it as a fallback option.
New in version 3.15:
Set
CMAKE_FIND_PACKAGE_PREFER_CONFIG
toTRUE
to tellfind_package()
to first search using Config mode before falling back to Module mode.
References: 1, 2.
Just use find_package
with CONFIG mode, check its result, and, if result is false, repeat the call with MODULE mode:
# First time do not use common *REQUIRED* but use QUIET for do not output error messages on fail.
find_package(XXX CONFIG QUIET)
if(NOT XXX_FOUND)
# Previous call has been failed. Fallback with MODULE mode.
find_package(XXX MODULE REQUIRED) # Now it is OK to use REQUIRED if needed.
# ... There could be additional actions for wrap result "as if" CONFIG mode.
endif()
# ... use XXX
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With