lizardsudoku
lizardsudoku

Reputation: 61

Setting up a Vulkan project with CMake on Windows

Until this point I have installed MinGW, CMake, and the Vulkan SDK. I also downloaded the GLFW precompiled binaries, GLM and PkgConfig according to this answer. Then I created a CMake project in CLion. This is the content of the CMakeLists.txt (which I got from here):

cmake_minimum_required(VERSION 3.16)
project(VulkanTest)

set(CMAKE_CXX_STANDARD 17)

add_executable(VulkanTest main.cpp)

find_package(Vulkan REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC ${Vulkan_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} Vulkan::Vulkan)

find_package(PkgConfig REQUIRED)
pkg_search_module(GLM REQUIRED glm)
include_directories(${GLM_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLM_LIBRARY_DIRS})

find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLFW_LIBRARIES})

The error message is as follows:

CMake Error at CMakeLists.txt:15 (find_package):
    By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
    asked CMake to find a package configuration file provided by "glfw3", but
    CMake did not find one.

    Could not find a package configuration file provided by "glfw3" (requested
    version 3.2) with any of the following names:

        glfw3Config.cmake
        glfw3-config.cmake

    Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
    "glfw3_DIR" to a directory containing one of the above files.  If "glfw3"
    provides a separate development package or SDK, be sure it has been
    installed.

I also tried replacing find_package(glfw3 3.2 REQUIRED) with pkg_search_module(GLFW REQUIRED glfw3) as described on the GLFW website, but I get the errors "None of the required 'glfw3' found" and "None of the required 'glm' found".

Upvotes: 6

Views: 13221

Answers (1)

Underdisc
Underdisc

Reputation: 323

First, the question, as this is likely what the all of the people visiting this thread are interested in. The problem demonstrated in the post seems very different from the question others probably want answered.

Answer For Visitors: You need to do three things in order to link with the Vulkan library on Windows when using cmake.

set(ENV{VULKAN_SDK} "Path/To/Vulkan/Version/Installation")
find_package(Vulkan REQUIRED)
target_link_libraries(target ${Vulkan_LIBRARIES})

The path should reference the specific version of Vulkan you are using. For me, this is C:/VulkanSDK/1.2.198.1, but it will be different for you depending on where Vulkan is installed and the version you want to use.

Don't forget to also add an include directory with something along the lines of target_include_directories(target PUBLIC "C:/VulkanSDK/1.2.198.1/Include") to avoid using absolute include paths for Vulkan headers in your code.

Explanation: The find_package command will search through a directory within your cmake installation for details about the package. For me, this directory is <cmake_install_dir>/share/cmake-3.18/Modules (3.18 should be replaced with the version you have installed.) In this directory you'll find a nice chunk of files named Find<PackageName>.cmake and FindVulkan.cmake should be among them. This file is what find_package is running under the hood. You'll notice a few instances of $ENV{VULKAN_SDK} in that file. This is why that VULKAN_SDK environment variable must be set before calling find_package. Cmake will throw errors if it isn't.

lizardsudoku's Problem (even though you probably already figured it out): As explained above, cmake is expecting to find a Findglfw3.cmake entry in that Modules directory and it didn't. Instead of creating one of those files yourself, it's easier to specify the glfw3 library directly in your CMakeLists.txt file like so.

list(APPEND CMAKE_PREFIX_PATH "path/to/lib/directory")
find_library(glfw NAMES glfw3 REQUIRED)
target_link_libraries(target ${glfw})

As someone mentioned, you want to make use of CMAKE_PREFIX_PATH to specify the directory the .lib is in. The find_library call can then search that directory for the glfw3.lib entry before it is specified as a linker input. Even though CMAKE_PREFIX_PATH affects what directories are searched through when find_package is used, the package file does not exist, hence the error does not change.

Upvotes: 11

Related Questions