Cmake Build Demo
The CMake build steps are as follows:
1. **Create a `CMakeLists.txt` file**: Define the project, targets, and dependencies.
2. **Create a build directory**: Keep the source code directory clean.
3. **Configure the project**: Use CMake to generate build system files.
4. **Compile the project**: Compile the project using the build system files.
5. **Run the executable**: Execute the generated program.
6. **Clean build files**: Delete intermediate files and target files.
Assume we have a simple C++ project that includes a main program file and a library file. We will use CMake to build this project.
Our project structure is as follows:
MyProject/βββ CMakeLists.txt βββ src/β βββ main.cpp β βββ mylib.cpp βββ include/ βββ mylib.h
* `main.cpp`: Main program source file.
* `mylib.cpp`: Library source file.
* `mylib.h`: Library header file.
* `CMakeLists.txt`: CMake configuration file.
### 1. Create the CMakeLists.txt File
Create a CMakeLists.txt file in the MyProject directory.
The CMakeLists.txt file is used to configure the CMake project.
**Content of CMakeLists.txt file:**
## Example
cmake_minimum_required(VERSION 3.10)# Specify minimum CMake version
project(MyProject VERSION 1.0)# Define project name and version
# Set C++ standard to C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Add include directories
include_directories(${PROJECT_SOURCE_DIR}/include)
# Add source files
add_library(MyLib src/mylib.cpp)# Create a library target named MyLib
add_executable(MyExecutable src/main.cpp)# Create an executable target named MyExecutable
# Link library to executable
target_link_libraries(MyExecutable MyLib)
**Explanation:**
* **`cmake_minimum_required(VERSION 3.10)`**: Specifies the minimum required CMake version as 3.10.
* **`project(MyProject VERSION 1.0)`**: Defines the project name as `MyProject` with version 1.0.
* **`set(CMAKE_CXX_STANDARD 11)`**: Sets the C++ standard to C++11.
* **`include_directories(${PROJECT_SOURCE_DIR}/include)`**: Specifies the directory for header files.
* **`add_library(MyLib src/mylib.cpp)`**: Creates a library target named `MyLib`, with the source file `mylib.cpp`.
* **`add_executable(MyExecutable src/main.cpp)`**: Creates an executable target named `MyExecutable`, with the source file `main.cpp`.
* **`target_link_libraries(MyExecutable MyLib)`**: Links the `MyLib` library to the `MyExecutable` executable.
### 2. Create a Build Directory
To keep the source code directory clean, we will create a separate build directory under the project root.
**Create Build Directory**
Open the terminal, navigate to the MyProject directory, and then create the build directory:
mkdir build cd build
### 3. Configure the Project
Use CMake to configure the project within the build directory.
This will generate platform-specific build system files (such as Makefiles).
**Run CMake Configuration**
Run the CMake configuration command inside the build directory:
cmake ..
* **`cmake ..`**: The `..` refers to the source code directory, which contains the `CMakeLists.txt` file. CMake reads the `CMakeLists.txt` file and generates build system files.
### 4. Compile the Project
Compile the project using the generated build system files. Depending on the type of build system file generated, use the corresponding build command.
**Using Makefile**
If a Makefile was generated (default on most Unix-like systems), you can compile using the make command:
make
* **`make`**: Compiles the project and generates the executable `MyExecutable`.
### 5. Run the Executable
After compilation, you can run the generated executable.
**Run Executable**
In the build directory, run MyExecutable:
./MyExecutable
### 6. Clean Build Files
Clean the build files to remove generated intermediate files and target files.
**Using make clean**
If cleanup rules were defined in CMakeLists.txt, you can use the make clean command:
make clean
* **`make clean`**: Removes intermediate files and target files.
**Manual Deletion**
If no cleanup rules were defined, you can manually delete all files in the build directory:
rm -rf build/*
YouTip