目标
要在Ubuntu 16.04
上面的vscode
去 debug C
,需要在vscode
上面去配置launch.json
以及task.json
。
像C
这种debug
,我们当然是希望每次都重新编译出来新文件再来debug
。
所以我们的流程就是 编译
- debug
。
工具
编译
需要用到 gcc
。
debug
需要用到 gdb
。
所以可以使用apt
安装上述包,建议顺带安装make
。
sudo apt install gcc make gdb
生成默认 launch.json
在生成好的配置文件加入字段preLaunchTask
,该字段设置是指,在执行当前文件内容的时候先运行一些任务。
这样子的话,就可以先让他编译
,我们再来debug
。
"preLaunchTask": "",
我们给这个新的task的名称是gcc build
。
而且需要更改下program
的值
"program": "enter program name, for example ${workspaceFolder}/a.out",
改成
"program": "${workspaceFolder}/a.out",
整个 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}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "gcc build", //注意这里
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
生成 task.json
你的界面可能是英文,但是无所谓,中英文的意思都是一样的。
然后我们关注现在tasks.json的核心字段。
"label": "echo", //标签,该任务的名字
"type": "shell", //用什么来解析执行下面的那个command
"command": "echo Hello" //命令
现在我们把他改一改。
"label": "gcc build", //这里需要跟launch.json里面的preLaunchTask的值一致,不然无法调用到指定的task
"command": "gcc", //gcc 来编译
"args": [ //gcc的运行参数
"-g",
"${file}",
"-o",
"a.out"
]
最终的tasks.json
如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "gcc build",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"a.out"
]
}
]
}
其他问题
你还可能会遇到类似以下的问题
glibc
在Ubuntu16.04
上面报错。
无法打开“ioputs.c”: 无法读取文件(Error: 找不到文件(vscode-remote://ssh-remote+xxxx/build/glibc-LK5gWL/glibc-2.23/libio/ioputs.c))。
提取问题的关键字段 — /build/glibc-LK5gWL/glibc-2.23/libio/ioputs.c
。
在这地方找不到glibc
,那么我们就给他下个。
sudo su
mkdir -p /build/glibc-LK5gWL/
cd /build/glibc-LK5gWL/
wget http://ftp.gnu.org/gnu/glibc/glibc-2.23.tar.gz
tar zxvf glibc-2.23.tar.gz