Column introduction
This column records the notes of bloggers getting started with CMake.
Welcome to the source code warehouse Star: https://github.com/Mculover666/cmake_study .
1. Overview of CMake
1. What is CMake
CMake official website: https://cmake.org/
CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.
CMake is an open source, cross-platform family of tools for building, testing and packaging software. CMake is used to control the software compilation process using simple platform- and compiler-independent configuration files, and to generate native makefiles and workspaces that can be used in the compiler environment of your choice. The CMake tool suite was created by Kitware in response to the need for a powerful cross-platform build environment from open source projects such as ITK and VTK.
2. Why do you need CMake?
Used to generate Makefile, suitable for file management of the upper SDK.
2. Install CMake
1. Windows installation
premise
You need to install MinGW, you can refer to my previous article: C/C++ development using Mingw-W64 on Windows (gcc toolchain) .
download
Download address: https://cmake.org/download/
Install
Note the option to add to the environment variable:
check version
After the installation is complete, check whether it is available:
2. Linux installation
3. Hello World
1. C source file
The simplest version of HelloWorld has only one C source file main.c:
the content is as follows:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello World By CMake\n");
return 0;
}
2. Write the simplest CMakeLists.txt
Create a new file CMakeLists.txt, write the following content, the simplest one build list only needs three lines of code.
(1) Set the latest required version of CMake
cmake_minimum_required(VERSION 3.26)
(2) Set the project name
project(HelloWorld)
(3) Add the executable target (HelloWorld) and its build source code (main.c)
add_executable(HelloWorld main.c)
3. Execute CMake
The execution format of CMake:
cmake <CMakeLists.txt所在路径>
Here you need to develop a habit. The compiled products are placed in a separate directory, create the cmake-build directory, and then go in and execute the cmake command.
mkdir cmake-build
cd cmake-build/
If executed directly, CMake generates a Visual Studio project by default:
cmake ..
Not what you want, empty the cmake-build directory.
4. Generate Makefile+gcc project
Specify the project to generate mingw:
cmake -G"MinGW Makefiles" ..
Check out the generated files:
5. Compile
Makefile has been generated, directly execute make to compile:
the generated executable file: