How can I make find_package search with config mod

2019-07-05 14:42发布

问题:

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.

回答1:

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


回答2:

You can try this find_package(XXX CONFIG REQUIRED).

see the link: CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?



标签: c++ cmake