The following notes are from: https://www.bilibili.com/video/BV13K411M78v?p=2
Existing problems: the compilation cannot be completed automatically, and you need to manually input commands to compile and connect them into executable files.
preparation
1. Installing software and compilers
How to install the compiler: https://www.cnblogs.com/TAMING/p/9945389.html
2. Install the following plug-ins on VScode
1, Compile c project
1. First create a folder with English path (including Chinese path, which may cause errors in compilation). The following methods can be used to create c/c + + files.
2. Write a simple program (pay attention to save it after compilation. If it is not saved, there is a small dot after the file name), and then create a new terminal and start compiling.
For specific gcc commands, see: https://wenku.baidu.com/view/90ae31d503d276a20029bd64783e0912a2167ca0.html
Here we use the simplest command (non debuggable):
gcc .\test.c -o test.exe
Including test C is the file to be compiled, - O test Exe indicates the name of the generated EXE file. If not, the default a.exe will be generated
If we want to generate files that can be debugged, we must add '- g', for example
gcc -g .\test.c -o test.exe
If it is a C + + program, the corresponding two commands here are:
g++ .\test.cpp -o test.exe
g++ -g .\test.cpp -o test.exe
3. Run executable
Input (where the executable generated by test):
.\test.exe
The following is the operation effect
2, Online debugging (c + + program as an example)
Here we first enter a relatively complex program.
1. A cpp file
Function: exchange two variable data.
#include "iostream" using namespace std; void swap(int &a,int &b) { int temp; temp = a; a = b; b = temp; } int main(int argc,char **argv) { int val1 = 10; int val2 = 20; cout << "before swap " << endl; cout << "val1 = " << val1 << endl; cout << "val2 = " << val2 << endl; swap(val1,val2); cout << "val1 = " << val1 << endl; cout << "val2 = " << val2 << endl; return 0; }
Create the corresponding launch json,tasks.json file
When finished, it will be created automatically vscode folder and launch json,tasks.json file, and then press F5 to enter debugging.
Note: the current working range should be switched back to test c. Otherwise, the operation returns wrong.
2. Multiple cpp files
Function: exchange the data of two variables, and write the corresponding sub functions as separate files
The specific codes are as follows:
//Main program code #include "iostream" #include "swap.h" using namespace std; int main(int argc,char **argv) { int val1 = 10; int val2 = 20; cout << "before swap " << endl; cout << "val1 = " << val1 << endl; cout << "val2 = " << val2 << endl; swap(val1,val2); cout << "val1 = " << val1 << endl; cout << "val2 = " << val2 << endl; return 0; } //swap.h content #include "iostream" void swap(int &a,int &b); //swap.cpp content #include "swap.h" void swap(int &a,int &b) { int temp; temp = a; a = b; b = temp; }
Compile command: G + + - G \test. cpp . \swap. cpp -o test. exe
Note: write as many cpp files as there are
The previous steps of generating debugging files are the same as before, but the following errors will be reported.
resolvent:
Note: switch back to test CPP working range
3. Use cmake to compile multiple files and complete debugging
1. First create a cmakelists Txt file, write the corresponding code
2. Configuration corresponding file
Use the shortcut key Ctrl+shift+p to find cmake: configure – > GCC 8.1.0... A build folder will be automatically generated. After entering the folder, execute mingw32 make exe, will generate what we need exe file
3. According to the above implementation, online debugging can be completed
Note: the routine here is to directly create a task JSON file, after pressing F5, automatically complete the compilation, connect and enter debugging, but there is a problem with the self generated file and it cannot be run directly
Final simulation effect: