1.安装VS Code
下载地址https://code.visualstudio.com/download
2.安装C++扩展程序
详情请看https://code.visualstudio.com/docs/languages/cpp
https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/
打开VS Code后,输入ext install cpptools,等待安装完成。
3.当然打开目录,编辑源码了
4.使用g++生成可执行程序/动态连接库
https://code.visualstudio.com/Docs/editor/tasks#_defining-a-problem-matcher
创建task.json,下面是gcc命令,改成g++对应的命令即可
{
"version": "0.1.0",
"command": "gcc",
"args": ["-Wall", "helloWorld.c", "-o", "helloWorld"],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
最后按Ctrl+Shift+b
注:修改快捷键设置:
File->Preferences->Keyboard Shortcuts
5.使用gdb调试
当然第一步是要安装gdb
sudo apt-install gdb
1) task.json的args里一定要添加-g,完整的task.json类似如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["-o","da.exe","main.c","-g"],
"showOutput": "always",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
2) 先生成可执行程序,详见第4点
3) 单击左侧的调试按钮,出来的界面上,单击绿色三角(调试)符号,会生成launch.json,根据https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/,默认生成的launch.json包括2个配置:打开新的进程及附加(attach)到已运行的进程,分别是C++ Launch和C++ Attach
4)尽情的调试吧,发现VSCode的快捷键跟Windows下的一样,F10、F11……