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 退出方法