linux学习记录.6.vscode调试c makefile

时间:2025-03-31 22:06:13

参考

https://www.cnblogs.com/lidabo/p/5888997.html

task有更新,不能使用文章的代码。

多文件

终端

touch main.c hw.c hw.h

vscode hw.c

Vscode

打开文件夹

编写三个项目文件

 /*hw.c*/
#include "hw.h" void print()
{
printf("Hello World!\n");
}
 /*hw.h*/
#include <stdio.h> void print();
 /*main.c*/
#include "hw.h" int main()
{
print(); return ;
}

此时使用

在终端直接使用gcc

gcc main.c hw.c hw.h

生成 a.out 和 hw.h.gch

使用make

在项目目录下建立makefile    touch makefile

编写makefile文件

build : main.o hw.o
gcc -o build main.o hw.o
main.o : main.c hw.h
gcc -g -c main.c
hw.o : hw.c hw.h
gcc -g -c hw.c
clean :
rm main.o hw.o

PS:clean下的代码需要使用 make clean 才调用

  -g :调试使用

  -c  :仅编译(Compile),不连接 (Make)

  -o :输出文件名

VS code 调试 配置

{
/*launch.json*/
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/build",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
], "preLaunchTask": "make" //add } ]
}
{
/*task.json*/
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make",
"type": "shell",
"command": "make",
"args": [] } ]
}

ctrl + shift + b 编译

设置断点

f5 开始调试

弹出终端 显示输出。