# Create a New Vulkan Project Using Conan and CMake
## Metadata
**Status**:: #x
**Zettel**:: #zettel/fleeting
**Created**:: [[2024-04-17]]
## Tools Installation
> [!file] Windows
```
scoop install cmake conan vulkan
```
Although there are Conan packages for Vulkan ([vulkan-headers](https://conan.io/center/recipes/vulkan-headers?version=1.3.268.0) and [vulkan-loader](https://conan.io/center/recipes/vulkan-loader?version=1.3.268.0)), it's recommended to install SDK since it also include other useful development tools.
## CMake Init
Set up the basic project structure
```
├── CMakeLists.txt
└── src
└── main.cpp
```
> [!file] CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.29)
oject(vulcan-cubes CXX)
add_executable(${PROJECT_NAME} src/main.cpp)
```
> [!file] main.cpp
```cpp
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
```
Check the hello word app:
```shell
mkdir build
cd build
cmake ..
cmake --build .
.\Debug\vulcan-cubes.exe
```
## Add Vulkan Dependencies Via Conan and CMake
Create the default compiler profile if it does not already exist.
```shell
conan profile detect --force
```
Create a file `conanfile.txt` to declare the dependencies
> [!file] conanfile.txt
```
[requires]
glfw/3.4
glm/0.9.9.8
[layout]
cmake_layout
[generators]
CMakeDeps
CMakeToolchain
```
Install the dependencies
```shell
conan install . --build=missing --conf=tools.env.virtualenv:powershell=True
```
The powershell config can be added to the Conan profile file:
```ini
[conf]
tools.env.virtualenv:powershell=True
```
Generate the build files for the project
```powershell
cmake --preset conan-default -B build
```
Update `CMakeLists.txt`
> [!file] CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.29)
project(vulcan-cubes CXX)
find_package(Vulkan REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} Vulkan::Vulkan glfw)
```
> [!hint]
> - I have to add include directories manually.
> - I have to add the glfw3 library explicitly instead of using the target `glfw` in Windows.
Use the dependencies in `main.cpp`
> [!file] main.cpp
```cpp
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream>
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
```
Build and run the demo
```shell
cmake --build build
.\build\Debug\vulkan-cubes.exe
```
## References
- florianvazelle on GitHub. (n.d.). _florianvazelle/VulkanStarter: A template for Vulkan C++ projects with GLFW, GLM and ImGUI using CMake, CI, Conan and doctest_. GitHub. Retrieved April 17, 2024, from https://github.com/florianvazelle/VulkanStarter
- CMake Authors. (n.d.). _Step 1: A Basic Starting Point — CMake 3.29.2 Documentation_. CMake Tutorial. Retrieved April 17, 2024, from https://cmake.org/cmake/help/latest/guide/tutorial/A%20Basic%20Starting%20Point.html
- Vulkan Tutorial Authors. (n.d.). Development environment. Vulkan Tutorial. Retrieved April 17, 2024, from https://vulkan-tutorial.com/Development_environment