- Create a new project directory, such as hello world;
- Create a new cpp file under the directory HelloWorld, such as helloworld.cpp;
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
- To compile a file, choose terminal > configure default build task > G + + build active file from the menu. This operation will automatically create the directory. vscode in helloworld and the tasks.json file in the directory. The contents are
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- Run the program, press the shortcut key Ctrl+Shift+B, switch to the project directory in the terminal and run. / helloworld;
- To debug the program, press F5 and select g++ build and debug active file. vscode will create the launch.json file as follows
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
- For other configurations of C/C + +, press Ctrl+Shift+P, and select C/C++: Edit Configurations (UI). vscode will automatically create the configuration file C ﹣ CPP ﹣ properties.json in the. vscode directory.