CMake is a build process management tool that works in an operating system in a compiler-independent manner. It makes use of CMakeLists.txt
to build project solutions. In this section, we will learn the process of building a CMake file for our first Vulkan application. Refer to the following instructions to understand the creation of this configuration file (CMakeLists.txt
):
CMakeLists.txt
file as per the specified folder structure convention, that is, chapter_3 > Sample Name > CMakeLists.txt
. For the purpose of ensuring compatibility across different CMake versions, you need to specify the minimum supported version. If the current version of CMake happens to be lower than the specified one, then it will stop building the solution. The minimum supported version of CMake is specified with cmake_minimum_required
. The following is the code from the CMakeList.txt
file:cmake_minimum_required(VERSION 3.7.1)
set
CMake keyword. Also, provide a meaningful name:set (Recipe_Name "3_0_DeviceHandshake")
# AUTO_LOCATE_VULKAN - accepted value ON or OFF # ON - Use CMake to auto locate the Vulkan SDK. # OFF - Vulkan SDK path can be specified manually. # This is helpful to test the build on various Vulkan version. option(AUTO_LOCATE_VULKAN "AUTO_LOCATE_VULKAN" ON ) if(AUTO_LOCATE_VULKAN) message(STATUS "Attempting auto locate Vulkan using CMake......") # Find Vulkan Path using CMake's Vulkan Module # This will return Boolean 'Vulkan_FOUND' indicating # the status of find as success(ON) or fail(OFF). # Include directory path - 'Vulkan_INCLUDE_DIRS' # and 'Vulkan_LIBRARY' with required libraries. find_package(Vulkan) # Try extracting VulkanSDK path from ${Vulkan_INCLUDE_DIRS} if (NOT ${Vulkan_INCLUDE_DIRS} STREQUAL "") set(VULKAN_PATH ${Vulkan_INCLUDE_DIRS}) STRING(REGEX REPLACE "/Include" "" VULKAN_PATH ${VULKAN_PATH}) endif() if(NOT Vulkan_FOUND) # CMake may fail to locate the libraries but could be able to # provide some path in Vulkan SDK include directory variable # 'Vulkan_INCLUDE_DIRS', try to extract path from this. message(STATUS "Failed to locate Vulkan SDK, retrying again...") # Check if Vulkan path is valid, if not switch to manual mode. if(EXISTS "${VULKAN_PATH}") message(STATUS "Successfully located the Vulkan SDK: ${VULKAN_PATH}") else() message("Error: Unable to locate Vulkan SDK. Please turn off auto locate option by specifying 'AUTO_LOCATE_VULKAN' as 'OFF'") message("and specify manually path using 'VULKAN_SDK' and 'VULKAN_VERSION' variables in the CMakeLists.txt.") return() endif() endif() else() message(STATUS "Attempting to locate Vulkan SDK using manual path......") set(VULKAN_SDK "C:/VulkanSDK") set(VULKAN_VERSION "1.0.33.0") set(VULKAN_PATH "${VULKAN_SDK}/${VULKAN_VERSION}") message(STATUS "Using manual specified path: ${VULKAN_PATH}") # Check if manual set path exists if(NOT EXISTS "${VULKAN_PATH}") message("Error: Unable to locate this Vulkan SDK path VULKAN_PATH: ${VULKAN_PATH}, please specify correct path. For more information on correct installation process, please refer to subsection 'Getting started with Lunar-G SDK' and 'Setting up first project with CMake' in Chapter 3, 'Shaking hands with the device' in this book 'Learning Vulkan', ISBN - 9781786469809.") return() endif() endif()
project
keyword you can specify any desire name of your project. On Windows, Window System Integration (WSI) needs the VK_KHR_WIN32_SURFACE_EXTENSION_NAME
extension API. For this, you need to define the VK_USE_PLATFORM_WIN32_KHR
preprocessor directives (with -D prefixed) in the CMake file using add_definitions()
. Include the path where Vulkan header files are placed. Also, add the path of the Bin folder to link the necessary Vulkan runtime/static libraries:# Specify a suitable project name project(${Recipe_Name}) # Add preprocessor definitions here add_definitions(-DVK_USE_PLATFORM_WIN32_KHR)
VULKAN_LIB_LINK_LIST
variable and later linked it to the building project using target_link_libraries()
. In addition, provide a correct path for including Vulkan header files using CMake’s include_directories()
API. Also, specify the path from where linked libraries are located using link_directories()
API.# Add ‘vulkan-1’ library for build Vulkan application. set(VULKAN_LINK_LIST "vulkan-1") if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") # Include Vulkan header files from Vulkan SDK include_directories(AFTER ${VULKAN_PATH}/Include) # Link directory for vulkan-1 link_directories(${VULKAN_PATH}/Bin) endif()
# Bunch the header and source files together
if (WIN32)
source_group ("include" REGULAR_EXPRESSION "include/*")
source_group ("source" REGULAR_EXPRESSION "source/*")
endif (WIN32)
file()
API and store them in the CPP_Lists
and HPP_Lists
variables. Use these lists to specify to the build solution all the files that need to be used for compilation. Provide a name to the project build and link it to all the necessary Vulkan libraries:# Define include path include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include) # Gather list of header and source files for compilation file (GLOB_RECURSE CPP_FILES ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) file (GLOB_RECURSE HPP_FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/*.*) # Build project, provide name and include files to be compiled add_executable (${Recipe_Name} ${CPP_FILES} ${HPP_FILES}) # Link the debug and release libraries to the project target_link_libraries (${Recipe_Name}${VULKAN_LIB_LIST})
# Define project properties set_property(TARGET ${Recipe_Name} PROPERTY RUNTIME_OUTPUT_- DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/binaries) set_property(TARGET ${Recipe_Name} PROPERTY RUNTIME_OUTPUT_- DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/binaries) set_property(TARGET ${Recipe_Name} PROPERTY RUNTIME_OUTPUT_- DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/binaries) set_property(TARGET ${Recipe_Name} PROPERTY RUNTIME_OUTPUT_- DIRECTORY_MINSIZEREL ${CMAKE_CURRENT_SOURCE_DIR}/binaries) set_property(TARGET ${Recipe_Name} PROPERTY RUNTIME_OUTPUT_- DIRECTORY_RELWITHDEBINFO ${CMAKE_CURRENT_SOURCE_DIR}/binaries) # Define C++ version to be used for building the project set_property(TARGET ${Recipe_Name} PROPERTY CXX_STANDARD 11) set_property(TARGET ${Recipe_Name} PROPERTY CXX_STANDARD_REQUIRED ON) # Define C version to be used for building the project set_property(TARGET ${Recipe_Name} PROPERTY C_STANDARD 99) set_property(TARGET ${Recipe_Name} PROPERTY C_STANDARD_REQUIRED ON)
Follow these steps to build the CMake file:
build
directory. If it is not present, create it. This empty build
folder will contain the Visual Studio project built through the command line. You can also use the CMake GUI instead. cmake -G "Visual Studio 14 2015 Win64" ..
This is how the command-line interface looks:
The two dots at the end of the command specify the path of CMakeLists.txt
(one folder level up), which is required by the CMake command to build the project. Upon successful execution, you will find the following project files based on the project name specified:
The following diagram shows the folder structure of all the samples that follow in this book:
18.219.228.88