Configuring VSCode development environment under linux
We must first of its profits
- System: Manjaro
- Kernel version: 5.4.24
- gcc Version: 9.2.1
- VSCode:1.43.0
VSCode Download
Since my Linux machine is Manjaro (a version of ArchLinux), installation VSCode traditional way and also Debian and RedHat-based system is not the same.
Arch versions can be mounted by normally
$ sudo pacman -S visual-studio-code-bin
But I trust that prompted the signature is unknown, the file is corrupted. So Shangguan net direct download archive.
Download the tar.gz
archive and unpack.
Preparing the environment
- Create a project directory
$ cd ~
$ mkdir -p workspace/clang/helloworld # 创建helloworld目录
$ cd workspace/clang/helloworld
- Installation compilation environment
VSCode just an editor, compiler work still needs to provide users with the build environment.
Use version
or whereis
to ensure that your computer is installed g++
,gdb
$ g++ --version
$ gdb --version
$ whereis g++
$ whereis gdb
Here I start the computer whereis gdb
showed the following path, but can not call version
this is not enough
$ whereis gdb
gdb: /usr/share/gdb
If not, then use the corresponding command to install linux series, as follows Manjaro,
$ pacman -S g++ gdb
Start VS Code
Switch to the project folder directory, start vscode, or directly to the directory folder into vscode.
The next operation will be automatically established under a project directory .vscode
folder, and built on the folder, which does not require manual
c_cpp_properties.json
: Compiler path configurationtask.json
: Command compile time configurationlaunch.json
: Debug Configuration
write the code
- New
helloworld.cpp
file, write the first program of each languageHello world
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "Hello, world! from VS Code!" << endl;
}
- Installation C / C ++ plug-ins
On the left side of the plug-in option card, search for C ++, the Microsoft installation C/C++
on the line
Back helloworld.cpp
then we create tasks.json
, tell VS Code how to compile the program.
- Through the menu
Terminal>Configure Default Build Task
. You will see a drop-down box, selectC/C++:g++ build active file
At this time, in your catalog will be more of a .vscode
folder, which is automatically generated tasks.json
.
Along the following lines
{
// task.json
"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
}
}
]
}
command
Used to set compiler, here is the g++
path. args
Is usually in the command line, g ++ command parameters that follow. Here the current file is compiled file $ {file}, the compiler output the same name in the file is the same path without the suffix helloworld
.
- Back
helloworld.cpp
through the menuTerminal>Run Build Task
to compile. If successful, the following input terminal
- Running in the terminal, enter the command
./helloworld
#include error
If the total is underlined C program and suggesting检测到#include错误。请更新includePath
- Through the
Ctrl+Shift+P
open command to navigate, run the commandC/C++: Edit Configurations(UI)
In includePath
adding, g ++ include position location
Command $ g++ -v -E -x c++ -
to view, simply add the two as shown
It will be generated for c_cpp_properties.json
files
debugging
- Through the menu
Run>Add Configuration
, chooseC++ (GDB/LLDB)>g++ build and debug active file
to create alaunch.json
file
File looks like
{
// launch.json
"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": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
Which miDebuggerPath
is the path of gdb, also it can be stopAtEntry
set true
so that a breakpoint is set automatically when entering the main function.
carry out
At this point, create a project, compiling, debugging may have been carried out in the VS Code.