In the past year, my work has been focused on native development, as I have spent most of my time working with cocos2dx. Therefore, future articles will lean towards C++.
Understanding CMake
Before introducing CMake, it’s necessary to cover some basic computer science concepts to better understand what CMake is.
How to Compile a Program
Programming languages are merely standard specifications that define syntax, while the job of a compiler is to convert source code into machine language.
Different operating systems and IDEs can freely choose which compiler to use. Common C++ compilers include: GCC (GNU Compiler Collection), MSVC series, llvm + Clang, TCC (Tiny C Compiler), Borland C++, etc.
If we are using the GCC compiler for program development, we need to manually compile the project in the command line. The general process is as follows:
# Manually compile source file
$ gcc -Wall -g -o hello hello.c
$ ./hello
# Execute
Hello world!
Using the compiler command directly to build a program can be considered the first generation of build tools.
How to Automate Program Building
If the project has a large number of source files, manually compiling them in the command line every time can be cumbersome. Hence, the make tool and Makefile were created, and it’s important to note that the two work together.
The make tool can be understood as a batch processing application, while the Makefile specifies how the program should be compiled and linked (essentially a configuration file). Using both in tandem allows for semi-automated program building.
Common make tools include: GNU’s gmake, Microsoft’s nmake, AutoMake, etc.
The advent of Makefile can be regarded as the second generation of build tools.
How to Develop Cross-Platform Programs
The rules for writing Makefile files are not compatible across different make tools. When developing cross-platform programs, it is impractical to write a Makefile for each platform. This led to the creation of cross-platform make tools, such as:
Qt's qmake: Tailored for QtScons: An automation build tool coded in Python; Godot Game Engine uses it for cross-platform compilation.xmake: An automation build tool coded in Lua, which is domestically produced and not widely used.Kitware's cmake: A build tool that uses CMakeLists.txt as its configuration, inventing its own syntax and currently being the mainstream build tool.
CMake can output various Makefiles (namke, gmake) and Project files (Visual Studio, Xcode). Both its learning curve and usability are relatively gentle, which is why most modern cross-platform game engines and open-source C/C++ projects choose CMake.
A significant feature of the third generation of build tools is cross-platform compatibility.
Conclusion
CMake can be understood as a toolkit that uses platform-independent configuration files to automatically compile and link programs, with the greatest advantage being its cross-platform capabilities.
CMake has almost become a standard for C/C++ projects.