find_package called before project() gives unhelpful error message
Consider the following CMakeLists.txt
file, which uses find_package
to find Vulkan:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
find_package(Vulkan REQUIRED)
project(hellovulkan)
project(hellovulkan VERSION 0.1)
add_executable(hellovulkan main.c)
configure_file(hellovulkan-config.h.in hellovulkan-config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${Vulkan_INCLUDE_DIRS})
target_link_libraries(hellovulkan glfw vulkan)
target_compile_definitions(hellovulkan PUBLIC GLFW_INCLUDE_VULKAN)
This yields the following error:
[Timur@timur-xps build]$ cmake ..
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_FIND_LIBRARY_PREFIXES
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_FIND_LIBRARY_SUFFIXES
CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
Could NOT find Vulkan (missing: Vulkan_LIBRARY Vulkan_INCLUDE_DIR)
Call Stack (most recent call first):
/usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake/Modules/FindVulkan.cmake:96 (find_package_handle_standard_args)
CMakeLists.txt:8 (find_package)
-- Configuring incomplete, errors occurred!
The error message Error required internal CMake variable not set, cmake may not be built correctly.
is very unhelpful and doesn't help me figure out how to solve this.
I'm new to CMake so I don't understand what the problem is, but after some trial and error I found out that if I put the line find_package
between the configure_file
and include_directories
then it starts working. I haven't a clue why.
Suggested solution: Please give a better error message which tells me where I need to put find_package
.
Edited by Brad King