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 開始調試

彈出終端 顯示輸出。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM