How can I add Vulkan to a cross platform CMake project?

10,528

If your find_package(Vulkan REQUIRED FATAL_ERROR) line is failing, you need to make sure the Vulkan SDK is properly installed, i.e. that you have a VULKAN_SDK environment variable that points to the correct location.

Additionally, do not embed the KhronosGroup/Vulkan-Hpp repository. This repository is for building the Vulkan C++ bindings, but shouldn't be used directly. Instead you should be using the vulkan.hpp header that is bundled with your installation of the Vulkan SDK. Otherwise when people try to build your project and have a different version of the Vulkan SDK installed than is referred to by your embedded KhronosGroup/Vulkan-Hpp

More generally, you are using find_package and then later you're using add_subdirectory to try to incorporate these external projects. That's not how it works. find_package will look for a pre-existing binary of the package, while add_subdirectory isn't designed to just swallow entire existing external CMake projects.

If you want to have your project build these others from source you should investigate the use of CMake's external project functionality. However, you'll probably find this to be more of a burden than it's worth. Alternatively, install vcpkg for your target platform, and use vcpkg to build and install glfw and sfml, then tell CMake to use the vcpkg dependencies (see the vcpkg docs on how to pass the CMAKE_TOOLCHAIN_FILE to your cmake configure line.

Share:
10,528
Roberto P. Romero
Author by

Roberto P. Romero

Updated on June 04, 2022

Comments

  • Roberto P. Romero
    Roberto P. Romero almost 2 years

    In order to make a CMake project as simple and as portable as it can get I have consider to add the "whole" repositories of the libraries I need to the project. The project structure is as follows:

    MyProject/
    └──CMakeLists.txt
    └──src/
        └──MyProject/
            └── *.h & *.cpp
            └── CMakeLists.txt
    └──ThirdParty/
        └──Vulkan-Hpp/
            └──(Vulkan Files)
        └──glfw/
            └──(glfw Files)
        └──SFML/
            └──(SFML Files)
    
    

    All the third party directories are the git submodules of the following repositories: https://github.com/KhronosGroup/Vulkan-Hpp

    https://github.com/SFML/SFML

    https://github.com/glfw/glfw

    Summarizing everything up, I'm having trouble integrating the vulkan and sfml libraries to the project.

    MyProject/CMakeLists.txt is as follows:

    cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
    
    project ("MyProject")
    
    set (MyProject_VERSION_MAJOR 0)
    set (MyProject_VERSION_MINOR 2)
    set (MyProject_VERSION_PATCH 1)
    
    set (CMAKE_CXX_STANDARD 17)
    
    # Include sub-projects.
    add_subdirectory ("src/MyProject")
    add_subdirectory ("ThirdParty/glfw")
    add_subdirectory ("ThirdParty/SFML")
    add_subdirectory ("ThirdParty/Vulkan-Hpp")
    

    MyProject/src/MyProject/CMakeLists.txt:

    cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
    
    project ("MyProject")
    find_package(Vulkan REQUIRED FATAL_ERROR) # error
    find_package(SFML REQUIRED network audio) # error
    find_package(glfw REQUIRED FATAL_ERROR) # error
    
    # Add source to this project's executable.
    add_executable (MyProject "MyProject.cpp")
    
    target_include_directories (MyProject 
        PUBLIC ${GLFW_INCLUDE_DIRS}
        PUBLIC ${SFML_INCLUDE_DIR}
        PUBLIC ${VULKAN_INCLUDE_DIRS}
    )
    
    target_link_libraries (MyProject glfw)
    target_link_libraries (MyProject ${VULKAN_LIB_LIST})
    target_link_libraries (MyProject ${SFML_LIBRARIES})
    

    How can I tweak CMake in order to use the third party libraries at my main project? Is the project structure incorrect?