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

参考

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

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

多文件

终端

touch main.c hw.c hw.h

vscode hw.c

Vscode

打开文件夹

 编写三个项目文件

1 /*hw.c*/
2 #include "hw.h"
3 
4 void print()
5 {
6     printf("Hello World!\n");
7 }
1 /*hw.h*/
2 #include <stdio.h>
3 
4 void print();
1 /*main.c*/
2 #include "hw.h"
3 
4 int main()
5 {
6     print();
7 
8     return 0;
9 }

此时使用

在终端直接使用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 开始调试

弹出终端 显示输出。

猜你喜欢

转载自www.cnblogs.com/protogenoi/p/9162549.html