reference resources
Link 1
Official course
Reference PDF:CMake practice Extraction code: tjyr
explain
The above link tutorial is directly aimed at the Linux system, which is slightly different from the Windows system. Continuous improvement with the learning process.
Personal programming environment: Win + VSCode + CMake 3.21.1
VSCode installed plug-in: CMake +CMake Tools
Create a new CMake folder under the English path to store the next test program
1 single source file
- Create a Demo1 folder in the CMake folder and open it through VSCode
- Create a new main.cpp file and a CMakeLists.txt file in the Demo1 directory (note the size of CMakeLists)
// main.cpp #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; return 0; }
// CMakeLists.txt # The minimum version number of CMake is determined by the version of CMake you download cmake_minimum_required(VERSION 3.21) #The name of the setting item is Hello, which can be set at will PROJECT(Hello) # Specify the generation target and compile the source file named main.cpp into an executable file named hello add_executable(hello main.cpp)
The commands in CMakeLists.txt are not case sensitive and # subsequent contents are considered comments
The command consists of the command name, parentheses, and parameters, separated by spaces
- Makefile files need to be generated
Press Ctrl + Shift +p, enter CMake in the search box, and select CMake:Configure
Select GCC suite instead of VS2019 suite
Wait a moment, a build directory will be automatically created under Demo1 directory. The build directory contains the required Makefile files
Note 1
In the tutorial shown in the link, enter the command cmake. To generate the Makefile file. However, this generation method fails in Windows + VSCode, and the Makefile file will not be generated.
-
In the terminal, modify the current working directory (Demo1) to the build directory. cd ./build
-
Continue to input the next command cmake
-
Continue to enter the next command mingw32-make.exe on the terminal. A hello.exe file will be automatically generated in the build directory
Note 2
In the tutorial shown in the link, enter the command make to generate the executable file hello.exe. However, this generation method also fails in Windows + VSCode.
In Windows, the mingw32-make.exe command is usually used to generate the executable file hello.exe.
To use the make command, you need to copy the mingw32-make.exe file and rename the backup to make.exe
Note: sometimes the mingw32-make.exe command needs to be modified appropriately by referring to the files contained in the bin directory in the downloaded MinGW
- Continue to input the next command. / Hello or. / hello.exe on the terminal to compile and run the program
2. Multiple source files in the same directory
- Create a Demo2 folder in the CMake folder and open it through VSCode
- Create new main.cpp, tem.cpp, tem.h and CMakeLists.txt files in Demo2
// main.cpp #include <iostream> #include "tem.h" using namespace std; int main() { int a = 10, b = 20; cout << "Before Swap" << endl; cout << "a=" << a << " " << "b=" << b << endl; fun(a, b); cout << "After Swap" << endl; cout << "a=" << a << " " << "b=" << b << endl; return 0; }
// tem.h #ifndef TEM_H #define TEM_H extern void fun(int &x, int &y); #endif
// tem.cpp void fun(int &x, int &y) { int temp = x; x = y; y = temp; }
// CMakeLists.txt cmake_minimum_required(VERSION 3.21) PROJECT(Demo) # Find all source files in the current directory (. Indicates the current directory) and save the name to DIR_SRCS variable aux_source_directory(. DIR_SRCS) #Indicator variable dir_ The source file in SRCs is compiled into an executable file named Demo2 #Variables are valued with ${} add_executable(Demo2 $)
3. Other operation sequences are similar to those of a single source file.
- First, generate the build directory with the help of shortcut keys
- Enter the build directory and enter the command cmake.. mingw32-make.exe to get the executable file Demo2.exe
- Finally, enter the. / Demo2 command to execute the executable Demo2.exe