1. 在wsl中打開vscode
- 進入wsl,找到程序目錄,在目錄下打開vscode
> cd /mnt/d/vscode/
> code .
ok,打開了vscode,並且進入了wsl。在vscode的左下角可以看到:
2. 在vscode中配置c++環境
創建main.cpp文件:
#include<stdio.h>
int main()
{
int a=0;
printf("hello %d\r\n",a++);
}
使用快捷鍵Ctrl+Shift+P顯示全部的命令。
2.1 配置c++環境(c_cpp_properties.json)
使用Ctrl+Shift+P,找到**C/C++:Edit Configuration (json)**點擊,會自動創建一個c_cpp_properties.json文件,內容如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
2.2 配置編譯任務(tasks.json)
使用Ctrl+Shift+P,找到Tasks:Configer Task點擊,再點擊Create tasks.json file from template,再選擇others會自動創建一個tasks.json文件:
修改tasks.json文件:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g","main.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
2.3 配置調試環境
點擊左側的Debug圖標,然後選擇Add Configurations再選擇C++(GDB/LLDB)
使用gcc-7 build and debug active file和gcc build and debug active file會自動創建相應的調試配置文件launch.json。使用Default Configuration需要自己手動修改文件如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
3. 調試方式
- F5 進入調試,或在下一個斷點暫停
- F10 單步調試
- F11 進入方法
- Shift + F5 停止調試
- Shift + F11 退出方法