Using CMake: A C++ Project Build Tool
CMake is a popular cross-platform build system that generates standardized build files, allowing C++ projects to be easily compiled and managed across different operating systems and development environments. In this article, we will introduce the basic usage of CMake and how to use it to create a simple C++ project.
What is CMake?
CMake is an open-source cross-platform build tool primarily designed to generate build scripts suitable for the target platform (such as Unix Makefiles, Visual Studio solutions, etc.) based on instructions in configuration files. It supports multiple programming languages but is most commonly used for C/C++ projects.
Installing CMake
On most Linux distributions, you can install it via the package manager:
sudo apt-get install cmake # Ubuntu/Debian
sudo yum install cmake # CentOS/RHEL
On Windows, you can download and install the precompiled version from the official website. For macOS users, you can install it via Homebrew:
brew install cmake
Creating a Simple C++ Project
Next, we will create a simple “Hello World” program and demonstrate how to use CMake to build this project.
1. Project Structure
First, create the following directory structure:
HelloWorld/
├── CMakeLists.txt
└── main.cpp
2. Writing Code
Enter the following code in <span>main.cpp</span>
:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
This is a very simple program that outputs “Hello, World!” to the console.
3. Writing the CMake Configuration File
Then, create a file named <span>CMakeLists.txt</span>
in the <span>HelloWorld/</span>
directory with the following content:
cmake_minimum_required(VERSION 3.10) # Set the minimum required CMake version
project(HelloWorld VERSION 1.0) # Define the project name and version
set(CMAKE_CXX_STANDARD 11) # Specify the use of C++11 standard
set(CMAKE_CXX_STANDARD_REQUIRED True) # Force the use of the specified standard
add_executable(hello main.cpp) # Specify the source code and generate the executable file hello
Configuration Explanation:
<span>cmake_minimum_required</span>
: Declares the minimum required version, which is at least 3.10.<span>project</span>
: Defines the current project name and its version.<span>set(CMAKE_CXX_STANDARD ...)</span>
: Sets which standard needs to be supported, here set to C++11.<span>add_executable(...)</span>
: Used to define which executables to generate and which source code is associated with them.
4. Building the Project with CMake
Open a terminal (or command prompt), navigate to your project root directory, and execute the following command sequence to complete the build process:
cd HelloWorld # Switch to the HelloWorld folder
mkdir build # Create a build folder to store temporary files and output results
cd build # Switch to the build folder
cmake .. # Call cmake to process the CMakeLists.txt file in the top-level directory
make # Execute the make command to start the compilation
If everything goes well, you will see messages indicating that the compilation process is starting. Ultimately, an executable file named <span>hello</span>
will be generated in the <span>build/</span>
directory.
5. Testing the Executable
Finally, you can run the generated executable file to verify the output:
./hello # Run this on Linux / macOS; on Windows, just run hello.exe
If you see the output “Hello, World!”, then you have successfully set up your first C++ project based on CMake!
Conclusion
This article introduced how to use CMake to streamline the C++ project build process from start to finish. It is particularly beneficial in modern development practices, as different IDEs and development tools can describe a unified interface. Additionally, we learned some basic parameter configurations and how to organize the entire project structure, enhancing our module reusability and laying the foundation for more complex applications in the future. We hope this tutorial helps everyone better understand and learn how to use this powerful tool.