背景: 公司要用vs code 在linux下跑通代码单步调试。下面是具体的操作。
安装环境:ubuntu 18.04, 系统自带的gcc 7.3.0
下载地址:https://code.visualstudio.com/
点击下载 .deb格式的文件
不需要修改,选择open with software install(default),点击ok。下载完成后,点击install,就可以安装了。
安装完成后,可以看到/usr/bin下有一个code可执行文件,直接在命令行输入code,就可以打开图像编辑界面。
按ctrl+p打开快速命令框,输入ext install cpptools
点击install安装插件,
安装完成后,需要点击reload,使安装生效。Reload之后的效果:
建议安装的插件:
c/c++:代码调试插件
code runner:必装,提供编译后程序的运行环境
c/c++ compile run :单文件编译
c/c++ snippets:代码自动补全
epitech c/c++ :为C/C++文件添加头部(包括作者、创建和修改日期等),并为.h头文件添加防重复的宏
c/c++ advanced lint :静态检查工具扩展
编写代码
自己建一个文件夹,路径最好是全英文的。使用vs code打开文件夹:
File-->open folder-->选择你的文件夹,打开,工作区域如下所示:
编写一段helloworld代码,命名为hello.cpp:
#include <stdio.h>
//#include <stdlib.h>
//#include <pthread.h>
int main()
{
printf("hello world !\n");
//system("pause");
int i = 0;
i += 10;
printf("this is the end of the world!\n");
return 0;
}
编写完代码,可以配置调试相关环境和具体的构建命令了。
修改配置文件
点击左侧第4个按钮,打开了调试界面。再点击工具栏带红点的齿轮按钮,选择C++(GDB/LLDB),会自动生成一个Launch.json.
需要修改launch.json配置文件为如下:
{
// 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}/bin/${fileBasenameNoExtension}.debug.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "hello_",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
工具栏 termital --> configure default build task --> create tasks.json file from template --> select a task template --> others
需要将自动生成的Tasks.json修改为如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "hello_",
"type": "shell",
"command": "gcc -g ${file} -o ${workspaceFolder}/bin/${fileBasenameNoExtension}.debug.out",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"presentation": {
"reveal": "always",
"panel": "new"
} // display output
}
]
}
设置code runner调试环境
code runner的修改步骤:
点击vs code的工具栏:File-->preferences-->settings-->输入 code runner-->edit in settings.json
默认是不能编辑的,需要点击左侧的修改图标(铅笔图标),在右侧出现的user settings中进行修改。
如下图,左侧是默认的,右侧是用户配置的。
在右侧中修改标红的2行即可。这里设置了编译的结果放在代码路径的bin目录下,所以,需要在代码路径下手动创建一个bin文件夹。
"c": "cd $dir && gcc $fileName -o bin/$fileNameWithoutExt.out && cd bin && ./$fileNameWithoutExt.out",
"cpp": "cd $dir && g++ $fileName -o bin/$fileNameWithoutExt.out && cd bin && ./$fileNameWithoutExt.out",
还需要设置2个勾选项,搜索相应的配置项,勾选即可:
"code-runner.runInTerminal": true,
"code-runner.ignoreSelection": true,
运行效果:
使当前编辑的文件为main()所在的.cpp文件,按F5,就可以看到运行的结果了。如需要调试,可以在需要的位置打断点进行调试。
右键-Run Code 或者按Ctrl+Alt+N,可以看到code runner运行的结果。
可能会遇到的问题:
linux vs code F5后,Stopping due to fatal error: NullReferenceException: Object reference not set to an instance of an object
解决方案:
Launch.json 下"externalConsole": false
快捷键
- Continue / Pause F5
- Step Over F10
- Step Into F11
- Step Out Shift+F11
- Restart Ctrl+Shift+F5
- Stop Shift+F5
Shift+Tab: 对齐缩进
Ctrl+Shift+D: debug view
Ctrl+Shift+E: File Explorer view
Ctrl+space 提示可用参数
参考链接:
https://blog.youkuaiyun.com/zhouyingge1104/article/details/80669995
https://www.cnblogs.com/lidabo/p/5888997.html
https://code.visualstudio.com/docs/editor/variables-reference
https://code.visualstudio.com/docs/editor/tasks-appendix
https://blog.youkuaiyun.com/qq_31359295/article/details/75113078
https://blog.youkuaiyun.com/qq_37968132/article/details/79685646
https://cutecoot.iteye.com/blog/1992595
https://github.com/Microsoft/vscode-cpptools/issues/2922