The content is very concise and practical, and there are practical operations at the end. You will understand after doing it once.
Compiler: gcc, clang
Tools: cmake, make
Instructions for use:
During development, use cmake to generate Makefile based on CMakeLists.txt.
Used when compiling, make compiles the project based on Makefile
cmake
CMake can automatically generate Makefile or project files for IDEs such as VS to achieve cross-platform compilation and construction.
cmake generates Makefile based on CMakeLists.txt, such as:
CMake is just a tool for generating compiler configuration files (makefiles)
1 在源代码目录下创建一个CMakeLists.txt文件,编写编译项目的规则。
2 进入一个新建的目录,例如build目录。
3 在build目录下执行cmake命令,指定源代码目录。
4 cmake会自动查找所需的编译工具,并生成一个Makefile文件。
5 在build目录下执行make命令,根据Makefile文件编译和链接源代码,生成可执行文件。
CMakeLists.txt
CMakeLists.txt is the rules for compiling the projectlike:
cmake_minimum_required(VERSION 3.23)
project(cnew C)
set(CMAKE_C_STANDARD 11)
add_executable(cnew
./main.c)
make
It is used to manage operations such as code compilation, linking, and installation, and can automatically complete the corresponding compilation and linking processes based on file dependencies. Its configuration file is Makefile.
make is a rule-based (Makefile) build tool
gcc
gcc is a C language compiler in the GNU compiler suite, often used to compile C code into executable files.
gcc main.c -o main
Actual operation
For example:
1. Generally, a c/cpp project is taken and the operations to be done are:
./configure
make
make install
This is the build process, make is used to build the project
2. File main.c written by yourself
gcc main.c -o main
This will be compiled into a binary executable file main
3. If you write another CMakeLists.txt file in the same path
cmake_minimum_required(VERSION 3.23)
project(cnew C)
set(CMAKE_C_STANDARD 11)
add_executable(cnew
./main.c)
At this time, there are two files in the current path, namely:
CMakeLists.txt and main.c.
Now it is time to use cmake.
cmake .
Executing the cmake . command will generate a Makefile and some other auxiliary files in the current directory. These auxiliary files include:
- CMakeCache.txt: Cache file, which records the variable values, paths and other information used when CMake generates Makefile -.
- cmake_install.cmake: installation file, containing information about the installation target file.
- Makefile: Makefile used to compile and link C/C++ projects.
- CMakeFiles directory: stores intermediate files generated by CMake during the build process, such as dependency files, compiler output, object files, etc.