Mastering CMake: A Step-by-Step Guide to Project Creation
Written on
Chapter 1: Introduction to CMake
In today's session, I aim to delve into the process of setting up projects with CMake. One of the key benefits of using CMake for my game development is its versatility in enabling cross-platform compatibility, streamlining version control, and adhering to established standards.
Installing CMake
To get started, I searched for CMake online and downloaded the appropriate installer for Windows x64. Once the download was finished, I executed the .exe file and made sure to add CMake to my system PATH for easy access.
Creating the Source Directory and CMakeLists.txt
Next, I created a directory intended for storing my CMakeLists.txt file, which outlines the parameters necessary for building my project. In this newly created folder, I added a text file and labeled it CMakeLists.txt.
After setting up the file, I right-clicked on the project and chose to open it with the Visual Studio Version Selector. Within Visual Studio, I included the following lines:
cmake_minimum_required(VERSION 3.29.0-rc4)
project(LightYears
VERSION 1.0.0
LANGUAGES C CXX
)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
These lines ensure that the minimum version required for this CMake project is the one I installed on my computer. Additionally, I specified the project name and version (1.0.0, indicating major changes, new features, and bug fixes) along with the programming languages in use. Lastly, I ensured compliance with the C++ standard by enforcing it and disabling any extensions.
Building the Project via Command Line
To build the project, I navigated to the location of the CMakeLists.txt file in the command prompt and created a new directory named "build." After changing into this newly created build folder, I typed the command:
cmake -S … -B .
This command builds the project for use in Visual Studio.
That's all there is to it! My project has been successfully created and configured in Visual Studio using CMake. Tomorrow, I plan to incorporate the game project files that I will utilize for my game design.
In the first video, titled "CMake: How to Build and Package C/C++ Projects," viewers will learn the fundamentals of building and packaging C/C++ applications using CMake.
The second video, "Better CMake Part 1 -- Basic Project Setup and Usage," provides a deeper insight into the basic project setup and usage of CMake, ideal for beginners.