Setting up a basic CMake project can seem daunting at first, but with a clear step-by-step guide, you’ll be through the setup process in no time. CMake is a powerful tool for managing project build configurations and it’s widely used in various software development environments. Follow these simple steps to create your first CMake project in 2025.
Step 1: Install CMake
Before starting your project, ensure that you have CMake installed on your system. You can download the latest version from the official CMake website. Follow the installation instructions provided for your Operating System. Verify your installation by typing cmake --version
in your terminal to ensure CMake is ready to use.
Step 2: Create Project Directory
Create a new directory for your project. This directory will contain all your source code and build files.
1 2 |
mkdir MyCMakeProject cd MyCMakeProject |
Step 3: Create the Source Code
Inside your project directory, create a simple main.cpp
file:
1 2 3 4 5 6 |
#include <iostream> int main() { std::cout << "Hello, CMake!" << std::endl; return 0; } |
Step 4: Write the CMakeLists.txt File
In the root of your project directory, create a CMakeLists.txt
file. This file will contain the necessary configurations for building your project.
1 2 3 4 5 6 |
cmake_minimum_required(VERSION 3.22) project(MyCMakeProject) set(CMAKE_CXX_STANDARD 17) add_executable(MyCMakeProject main.cpp) |
Step 5: Build the Project
Create a build directory and run CMake to generate the necessary build files:
1 2 3 |
mkdir build cd build cmake .. |
If everything is configured correctly, CMake will generate build files corresponding to your system’s native build tool.
Step 6: Compile and Run
Compile the code by running the build command:
1
|
cmake --build .
|
Once compiled, execute your program:
1
|
./MyCMakeProject
|
You should see “Hello, CMake!” output to your terminal.
Step 7: Explore More CMake Features
Once you have your basic project set up, you may want to explore more complex features of CMake such as printing working directories, using macros, handling wildcards during installation, and running custom commands. Check out these tutorials for more in-depth explanations:
- How to Print Working Directory in CMake
- How to Use Macro in CMake
- How to Use Wildcard in CMake Install
- How to Run a Basic add_custom_command in CMake
- How to Add Library Source in CMake
Conclusion
Congratulations! You’ve successfully created a basic CMake project. CMake is an extremely versatile tool that, once mastered, can greatly increase your efficiency in managing builds. As you grow more comfortable, you can start experimenting with its more advanced features by following the given tutorials.
Happy Coding! “`
By following this guide, you’ve taken the first steps into mastering the setup of CMake for building projects. With continuous exploration and patience, it’s only a matter of time before you become very adept at using CMake!