[CMake entry and advanced (1)] An example to understand what is CMakeLists - starting from "Hello World" (with code)

        In the previous content, we wrote many sample programs, but these sample programs only have one .c source file, which is very simple. Therefore, compiling these sample codes is actually very simple, just use the GCC compiler to compile directly, even Makefile is not needed. However, in actual projects, it is not so simple. A project may contain dozens, hundreds or even thousands of source files, which are placed in different directories according to their types, functions, and modules; A project usually uses the make tool to manage and compile. The make tool relies on the Makefile file to define the compilation rules of the entire project through the Makefile file, and uses the make tool to analyze the compilation rules defined by the Makefile.

        The benefit brought by Makefile is "automatic compilation". Once written, only one make command is needed, and the entire project will be automatically compiled according to the compilation rules defined in the Makefile, which greatly improves the efficiency of software development. Most IDEs have this tool, such as nmake of Visual C++, GNU make under linux, qmake of Qt, etc. These make tools follow different specifications and standards, and the syntax and format of the corresponding Makefile files are also different. This brings a serious problem: if the software wants to be cross-platform, it must be guaranteed to be compiled under different platforms, and if the above make tool is used, it is necessary to write a Makefile for each standard, which will be a problem. Crazy work.

        And cmake was born for this problem, allowing developers to write a platform-independent CMakeLists.txt file to formulate the compilation process of the entire project, and then generate localized Makefile and project files according to the specific compilation platform, and finally execute make compile.

        Therefore, for most projects, we should consider using more automated cmake or autotools to generate Makefiles instead of writing Makefiles directly.

        In this column, we will learn cmake, which is also the starting point of our project programming.

Introduction to cmake

        cmake is a cross-platform automatic build tool. The introduction has already been introduced to you. The birth of cmake is mainly to solve the problem that the direct use of make+Makefile cannot achieve cross-platform, so cmake can achieve cross-platform Compilation tools, this is its biggest feature, of course, in addition to this, cmake also includes the following advantages:

  • Open source code  We can download its source code directly from cmake official website https://cmake.org/ ;
  • Cross-platform  cmake does not directly compile and build the final executable file or library file, it allows developers to write a platform-independent CMakeLists.txt file to formulate the compilation process of the entire project, and the cmake tool will parse the CMakeLists.txt file Grammatical rules, and then according to the current compilation platform, generate localized Makefile and project files, and finally compile the entire project through the make tool; so it can be seen that cmake only generates corresponding Makefiles according to different platforms, and finally uses the make tool to Compile the project source code, but cmake is cross-platform.
  • The grammar rules are simple  . The grammar rules of Makefile are relatively complicated. For a beginner, it is usually not so friendly, and the grammar rules of Makefile are often different under different platforms; while cmake relies on the CMakeLists.txt file, the grammar of this file The rules have nothing to do with the platform, and the grammar rules are simple and easy to understand! The cmake tool automatically generates Makefile for us by parsing CMakeLists.txt, so we don't need to write Makefile manually.

cmake 和 Makefile

        Intuitively, cmake is a tool used to generate Makefile, parse CMakeLists.txt to automatically generate Makefile:

         In addition to cmake, there are some other automatic construction tools, such as automake, autoconf, etc., which are commonly used. Friends who are interested can find out by themselves.

How to use cmake

        cmake is a tool command, which can be installed online through the apt-get command in the Ubuntu system, as follows:

sudo apt-get install cmake

         After the installation is complete, you can view the version number of cmake through the cmake --version command, as follows:

         As can be seen from the above figure, the corresponding version number of cmake installed in the current system is 3.5.1, and the update of the cmake tool version is also relatively fast. From the official website of CMake, the latest version of cmake is 3.22.0, but this does not matter, which version is actually used It's all possible, and the difference will not be too big, so you don't have to worry about this.

        After installing the cmake tool, let's learn how to use cmake. The cmake official also provides you with corresponding tutorials, the link address is as follows:

Documentation | CMake                                                        //Document total link address       

CMake Tutorial — CMake 3.26.4 Documentation                 //Training tutorial

         If you have a strong self-learning ability, you can learn cmake by referring to the official training tutorials; for cmake learning, the author gives you two suggestions:

  • From simple to complex!
  • The point is to practice more by yourself.

        In this article, we will start with a very simple example to introduce how to use cmake, and then further expand from this example, put forward more requirements, and see how cmake can meet these requirements.

Example: Single Source File Compilation

        The program of a single source file is usually the simplest. How to build a classic C program "Hello World" with cmake?        

//main.c
#include <stdio.h>

int main(){
    printf("Hello World!\n");
    return 0;
}

        Now we need to create a new CMakeLists.txt file. The CMakeLists.txt file will be parsed by the cmake tool, just like the Makefile file will be parsed by the make tool; after the CMakeLists.txt is created, write the following content in the file:

project(HELLO)
add_executable(hello ./main.c)

        After writing, save and exit. The current project directory structure is as follows:

├── CMakeLists.txt
└── main.c

        There are two files in our project directory, the source file main.c and CMakeLists.txt, and then we directly execute the cmake command in the project directory, as shown below:

cmake ./

        The path carried behind cmake specifies the path of the CMakeLists.txt file, and the execution results are as follows:

         After executing cmake, in addition to the source files main.c and CMakeLists.txt, you can see that many other files or folders are generated in the current directory, including: CMakeCache.txt, CmakeFiles, cmake_install.cmake, Makefile, the focus is After generating this Makefile, with the Makefile, we then use the make tool to compile our project, as shown below:

        After compiling through make, an executable file hello is obtained. This name is specified in the CMakeLists.txt file, and I will introduce it to you later. Through the file command, you can see that hello is an executable file under the x86-64 architecture, so it can only be run on our Ubuntu PC:

        In order to verify whether the running result of the hello executable file is the same as the source code, we can run it directly under Ubuntu, as shown below:

CMakeLists.txt file

        Above we have demonstrated how to use cmake through a very simple example, the focus is to write a CMakeLists.txt file, now let's see what is written in the CMakeLists.txt file. 

        ⚫ The first line project(HELLO)

        project is a command, and the usage of the command is somewhat similar to the function in C language, because a pair of parentheses are required after the command, and we usually need to provide parameters, and multiple parameters are separated by spaces instead of commas ",".

        The project command is used to set the name of the project, and the parameter HELLO in the brackets is the name of the project we want to set; setting the project name is not mandatory, but it is best to add it.

        ⚫ The second line add_executable(hello ./main.c)

        add_executable is also a command to generate an executable file. In this example, two parameters are passed in. The first parameter indicates the file name corresponding to the generated executable file, and the second parameter indicates the corresponding source file; So add_executable(hello ./main.c) indicates that an executable file named hello needs to be generated, and the required source file is main.c in the current directory.

        Build using out-of-source

        In the above example, the files generated by cmake and the final executable file hello are mixed with the source file main.c of the project, which makes the project look very messy, and it will become very messy when we need to clean up the files generated by cmake Trouble, this is not what we want to see; we need to separate the files generated by the build process from the source files and not let them mix together, that is, use the out-of-source way to build.

        Clean up the files generated by cmake compilation, and then create a build directory under the project directory, as follows:

├── build
├── CMakeLists.txt
└── main.c

         Then go to the build directory and execute cmake:

cd build/
cmake ../
make

         In this way, the intermediate files generated by cmake and the executable files compiled by make are all in the build directory. If you want to clean up the project, just delete the build directory directly, which is much more convenient, as shown in the figure below.

Guess you like

Origin blog.csdn.net/cj_lsk/article/details/130983172