vscode下c调式
[日期:2021/4/28]
[作者:linhs]
[vscode] [c语言] [gdb]
简介
基于 ubuntu 21.04 x64
安装相关的工具
包 | 说明 |
---|---|
vscode | 代码编辑器 |
make | 编译命令 |
gdb | 程序调式器 |
gcc | 编译器 |
注:安装vscode从官网上下载,否则会出现输入法无法输入中文的现象
https://code.visualstudio.com/docs/editor/extension-marketplace
安装vscode的基本开发扩展包
vscode扩展id | 说明 |
---|---|
ms-ceintl.vscode-language-pack-zh-hans | 适用于 VS Code 的中文(简体)语言包 |
shd101wyy.markdown-preview-enhanced | Markdown Preview Enhanced |
ms-vscode.cpptools | C/C++ for Visual Studio Code |
linhs@linhs:~/work/rust$ sudo apt-get update
linhs@linhs:~/work/rust$ sudo apt-get upgrade
linhs@linhs:~/work/rust$ sudo apt-get install gcc
linhs@linhs:~/work/rust$ sudo apt install gdb-multiarch openocd qemu-system-arm
创建工程
使用vscode打开一个工作文件夹 c_debug
graph LR
A[c_debug] ----> B[src]
A ----> C[lib]
A ----> D[target]
A ----> E[Makefile]
B ----> main.c
C ----> lib_test.c
C ----> lib_test.h
D ----> X[debug]
X ----> src ----> main.o
X ----> lib ----> lib_test.o
X ----> out
接下来建立这些目录和文件
./src/main.c
./lib/lib_test.c
./lib/lib_test.h
./target/debug/src/
./target/debug/lib/
./Makefile
文件内容:
./src/main.c
#include <stdio.h>
#include "lib_test.h"
int main(int argc, char *argv[])
{
printf("[main] debugging\n");
test();
return 0;
}
./lib/lib_test.c
#include <stdio.h>
void test(void)
{
printf("[test] debugging\n");
}
./lib/lib_test.h
#ifndef __TEST_H___
#define __TEST_H___
extern void test(void);
#endif
./Makefile
CC := gcc
TARGET_DIR := ./target/debug
TARGET := $(TARGET_DIR)/out
OBJS := $(TARGET_DIR)/src/main.o
OBJS += $(TARGET_DIR)/lib/lib_test.o
INC := -I ./lib
CONFIG := -g
$(TARGET_DIR)/%.o: %.c
$(CC) $(CONFIG) -c -o $@ $< $(INC)
$(TARGET): $(OBJS)
$(CC) $(CONFIG) -o $(TARGET) $^
all: $(TARGET)
@echo "build finish"
clean:
rm -rf $(TARGET) $(OBJS)
执行make all,编译成功后启动调式
Start Debugging -> C++(GDB/LLDB)
此时生成配置文件
./vscode/launch.json
./vscode/tasks.json
首先是修改 ./vscode/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": "gcc-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc-10 build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
其中的属性 “program” 是gdb要调式的文件,将属性指定到 “program”: “${workspaceFolder}/target/debug/out”; 另外属性"externalConsole" 可改为ture,当为true时,启动调式会打开另一新的终端进行调试;最后改为如下:
{
// 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": "gcc-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc-10 build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
注:如果只是调式一个.c文件就不需要做任何修改,直接打开.c文件进行调式
接下来是修改 ./vscode/tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc-10 build active file",
"command": "/usr/bin/gcc-10",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
./vscode/launch.json属性"label"应该和 ./vscode/tasks.json属性"preLaunchTask"值一样,这样gdb启动时才能调用编译任务
最后修改如下
{
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc-10 build active file",
"command": "make",
"args": [
"all",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
将属性 “command"改为 “make”, “args” 改为"all”, “type” 改为"shell";这样使用Makefile进行编译
注:如果只是调式一个.c文件就不需要做任何修改,直接打开.c文件进行调式
再点击 Start Debugging 调式
默认情况下程序之间运行的结束,甚至无法放置断点
打开设置 输入 debug.allowBreakpointsEverywhere 属性改为 true
注:“debug.allowBreakpointsEverywhere”:true, 允许在任何文件中设置断点