Fabricio Manrique
Fabricio Manrique

Reputation: 23

Vulkan HPP with cmake

I'm trying to include vulkan hpp library using cmake with fetch_content (I want to automate this and I don't want the user to manually install vulkan, if this is a wrong approach let me know because I'm just starting with cmake) as shown in the following code snippet

include(FetchContent)

FetchContent_Declare(
        vulkanhpp
        GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Hpp.git
        GIT_TAG b97783be3ed973f4e0d96fc7c680920f5ab0aa3a #1.2.191
)

FetchContent_GetProperties(vulkanhpp)
if ( NOT vulkanhpp_POPULATED )
    message ( STATUS "Fetching vulkanhpp" )
    FetchContent_Populate(vulkanhpp)

    set ( SAMPLES_BUILD OFF CACHE INTERNAL "Build the vulkan hpp samples" )
    set ( VULKAN_HPP_INSTALL OFF CACHE INTERNAL "Install vulkan hpp" )

    add_subdirectory ( ${vulkanhpp_SOURCE_DIR} ${vulkanhpp_BINARY_DIR} )
endif ()

Now my problem is that I don't know how to include the "include" folder in my main.cpp file when I put in the cpp file:

#include "vulkan/vulkan.hpp"

without putting the full path in CMakeLists.txt and instead using some CMake variable

My other cmake list is as follows

include_directories ( "" ) ### what should I put here for include vulkan hpp library ###

# Add source to this project's executable.
add_executable ( example "main.cpp" )

Is this the correct way for automatic managing the vulkan hpp dependency? Are there any other alternative without using find_package?

Upvotes: 2

Views: 2486

Answers (2)

Jean-Christophe
Jean-Christophe

Reputation: 697

In order to compile, Vulkan C++ Headers (Vulkan HPP) needs to include Vulkan C Headers (Vulkan Headers). Here is an example to fetch the vulkan stack with optional GLAD and GLFW.

include(FetchContent)
# Declare required Vulkan version
set(VULKAN_VER_MAJOR 1)
set(VULKAN_VER_MINOR 3)
set(VULKAN_VER_PATCH 278)
set(VULKAN_VERSION ${VULKAN_VER_MAJOR}.${VULKAN_VER_MINOR}.${VULKAN_VER_PATCH})

# Declare GLAD version
set(GLAD_VERSION 2.0.5)

# Declare GLFW3 version
set(GLFW3_VERSION 3.4)

#-------------------------------------------------------------------------
# Fetch GLAD
# ------------------------------------------------------------------------
message(STATUS "Include GLAD")
FetchContent_Declare(
        libglad
        GIT_REPOSITORY https://github.com/Dav1dde/glad.git
        GIT_TAG v${GLAD_VERSION}
        GIT_SHALLOW TRUE
        GIT_PROGRESS TRUE
)

FetchContent_GetProperties(libglad)
FetchContent_Populate(libglad)
set(GLAD_SOURCES_DIR ${libglad_SOURCE_DIR})
add_subdirectory(${libglad_SOURCE_DIR}/cmake ${CMAKE_CURRENT_BINARY_DIR}/cmake)
glad_add_library(glad_vulkan SHARED REPRODUCIBLE LOADER API gl:core=4.6 vulkan=${VULKAN_VER_MAJOR}.${VULKAN_VER_MINOR})

#-------------------------------------------------------------------------
# Fetch GLFW3
# ------------------------------------------------------------------------
message(STATUS "Include GLFW3")
FetchContent_Declare(
        libglfw
        GIT_REPOSITORY https://github.com/glfw/glfw.git
        GIT_TAG ${GLFW3_VERSION}
        GIT_SHALLOW TRUE
        GIT_PROGRESS TRUE
)

set(GLFW_INSTALL OFF)
set(BUILD_SHARED_LIBS ON)
set(GLFW_BUILD_DOCS OFF)
set(GLFW_BUILD_COCOA OFF)
set(GLFW_BUILD_WAYLAND OFF)
FetchContent_MakeAvailable(libglfw)

#-------------------------------------------------------------------------
# Fetch Vulkan C Headers
# ------------------------------------------------------------------------
message(STATUS "Include Vulkan C Headers")
FetchContent_Declare(
        vulkanheaders
        GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
        GIT_TAG v${VULKAN_VERSION}
        GIT_SHALLOW TRUE
        GIT_PROGRESS TRUE
)
set(PROJECT_IS_TOP_LEVEL OFF)
FetchContent_MakeAvailable(vulkanheaders) # vulkanheaders has a proper CMakeLists

#-------------------------------------------------------------------------
# Fetch Vulkan C++ Headers
#-------------------------------------------------------------------------
message(STATUS "Include Vulkan C++ Headers")
FetchContent_Declare(
    vulkanhpp
    GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Hpp.git
    GIT_TAG v${VULKAN_VERSION}
    GIT_SHALLOW TRUE
    GIT_PROGRESS TRUE
)
# vulkanhpp does not have a proper CMakeLists
FetchContent_GetProperties(vulkanhpp)
FetchContent_Populate(vulkanhpp)

# Define a library for Vulkan
add_library(vulkan INTERFACE)
target_include_directories(vulkan INTERFACE ${vulkanhpp_SOURCE_DIR})
target_link_libraries(vulkan INTERFACE Vulkan::Headers glfw glad_vulkan)

Then you simply need to link vulkan to your own library or executable.

Upvotes: 0

octowaddle
octowaddle

Reputation: 93

The repository you specified does not actually contain the Vulkan headers. Use this instead. It provides a CMakeLists.txt file which adds the headers to a library called Vulkan::Headers so you can just add the subdirectory and then link to them using target_link_libraries(example PRIVATE Vulkan::Headers). But why would you do this? To use Vulkan you have to have the Vulkan SDK installed (the actual libraries) and the SDK also includes the headers.

Upvotes: 0

Related Questions