Article directory
Preface
提示:本教程默认电脑安装Vscode 和 clang++
About whether clang++ is installed on the computer
Keyboard command + space and enter ter in the search box to select the terminal.
clang++ --version
The following figure appears and clang++ is installed locally.
1. Open VSCode and create a new folder
Create three folders in the root directory
- target stores compiled files
- src stores source code, that is, .cpp suffix file
- inc stores header files, which are files with .h suffix.
提示: 目录结构一定建立要正确
2. Install the plug-in
- Install and search for C/C++ as shown in the figure
- Install code runner
- Install CodeLLDB
3. Generate configuration files
- Create hello.cpp file under src
- Paste the following code in hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout<<"hello world"<<endl;
return 0;
}
- Generate task.json
- Click the title button
2. Click clang++ to generate the active file.
3. You will create the .vscode file yourself to generate task.json.
Modify the task.json parameters.主要能够编译多文件 也就能够引入自定义头文件
Before modification
After modification
"-g",
"${fileDirname}/*.cpp",
"-o",
"${workspaceFolder}/target/${fileBasenameNoExtension}"
- Generate launch.json file
- Click as shown in the picture
to modify launch.json
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
-
Create c_cpp_properties.json.
Press shaift + command + p (⇧⌘P) and click Edit Con...
-
Set the code runner
to create settings.json in .vacode
{
"C_Cpp.errorSquiggles": "disabled",
"files.associations": {
"__locale": "c",
"__string": "c",
"string": "c",
"string_view": "c",
"cstring": "c",
"locale": "c",
"max.h": "c",
"ostream": "cpp"
},
"code-runner.runInTerminal": false
}
Then set it up as shown in the figure
覆盖掉原来 cpp
"cpp": "cd $dir && g++ *.cpp -o $workspaceRoot/target/$fileNameWithoutExt && $workspaceRoot/target/$fileNameWithoutExt",
At this point, all configurations are completed. Next, run
Four, run
- Run the code you just pasted in hello.cpp, right-click -> RunCode
这里需要注意 结果 输出 这个不能写也就是程序有输入代码不可以 如果要输入东西 在 .vscode -> settings.json 修改 "code-runner.runInTerminal": true 即可
Test custom header files
- Create the swap.h file in the inc folder
#include <iostream>
using namespace std;
void swap(int a,int b);
- Create swap.cpp under src
#include "../inc/swap.h"
void swap(int a,int b){
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
- Modify hello.cpp
#include <iostream>
#include "../inc/swap.h"
using namespace std;
int main()
{
cout << "hello" << endl;
int a = 10;
int b = 20;
swap(a,b);
}
Right click and run
Test multi-file debugging
Finally came in
注意: 如果你同一目录下 多个 .c / .cpp文件里都有 main函数 / std命名空间 要单独建立文件夹里面放目标文件 如图 (这里c截图演示)
Summarize
OK, you're done. If it helps you, please give it a like. Thank you.