[question] Is CMAKE_FIND_PACKAGE_PREFER_CONFIG=ON falling back to Module mode?
OS: macOS Sonoma CMake version: 3.26.4
Having this CMakeLists.txt and hdf5
installed in your system (in my case brew install hdf5
):
cmake_minimum_required(VERSION 3.20)
project(proof LANGUAGES CXX)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
find_package(HDF5 REQUIRED)
add_executable(proof proof.cpp)
target_link_libraries(proof PRIVATE HDF5::HDF5)
I ran into errors like this:
$ mkdir build && cd build
$ cmake .. -GNinja
-- The CXX compiler identification is AppleClang 15.0.0.15000309
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.7s)
CMake Error at CMakeLists.txt:9 (target_link_libraries):
Target "proof" links to:
HDF5::HDF5
but the target was not found. Possible reasons include:
* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.
-- Generating done (0.0s)
CMake Generate step failed. Build files cannot be regenerated correctly.
However, if I unset that variable set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF)
, it runs OK:
-- The CXX compiler identification is AppleClang 15.0.0.15000309
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found HDF5: hdf5-shared (found version "1.14.3")
-- Configuring done (0.7s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/user/proof/build
My question is about the CMake documentation is saying:
Set CMAKE_FIND_PACKAGE_PREFER_CONFIG to TRUE to tell find_package() to first search using Config mode before falling back to Module mode.
So, is that behavior expected and the documentation is not clear? or is it a CMake bug?
I expected CMake to fall back to Module mode without unsetting the variable.