記錄一次VSCode配置C/C++編譯調試

操做是在windows環境,linux應該操做差很少

0x01 安裝minGW

我這裏用到的是minGWlinux

安裝完成後還須要添加make,執行下面命令,由於我須要用到Makefile文件shell

mingw-get install mingw32-make

0x02 配置調試

先來試試調試,按下F5的時候若是你沒有launch.js文件,則會提示你建立一個,而後選擇 C++(GDB/LLDB)
json

建立以後先編譯一個程序試試windows

#include <stdio.h>
int main(){
    printf("Hello,World!\n");
    char a=getchar();
    printf("input char:%c\n",a);
    return 0;
}
//編譯加 -g 參數
//gcc -g heelo.c -o hello
//在同一目錄下生成 hello.exe

配置launch.jsonui

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",//調試的名字
            "type": "cppdbg",
            "request": "launch",
            "program": "hello.exe",//程序的目錄
            "args": [],//參數 無論
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",//運行目錄
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "E:\\mingw\\bin\\gdb.exe",//gdb目錄
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

而後再在哪一個文件按下F5,就能夠愉快的下斷點調試了,左側能夠查看變量的值
spa

0x03 make編譯調試

而後由於我有一些其餘的想法,用到了Makefile,我得先make一次,而後再編譯,若是按照上面的方法就得先在cmd裏面輸入一次make,而後再f5調試,我想直接f5,一步完成,最主要的是在launch.json裏面增長一項配置preLaunchTask,配置先執行的任務
先寫一下Makefile3d

g=gcc -g
obj=hello.o
hello:$(obj)
    $(g) $(obj) -o hello
hello.o:
    $(g) -c hello.c

make一次,還不錯,而後改一下launch.json文件調試

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\hello.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "E:\\mingw\\bin\\gdb.exe",
            "preLaunchTask": "build",//先運行的任務build是任務名字
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

而後會提示你建立一個task.json的文件code

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "mingw32-make",//執行的命令,make文件路徑 我這裏配置了環境變量,因此能夠直接寫
            "args": ["hello"]//執行的時候後面附加的參數
        }
    ]
}

而後F5的時候,就能夠直接調試啦,下面終端是顯示的執行的命令
orm

我這裏沒寫args,寫了後終端應該顯示 > Executing task: mingw32-make hello <

而後記錄一下VSCode的一些自帶變量

官方文檔:https://code.visualstudio.com/docs/editor/variables-reference

${workspaceRoot} 當前打開的文件夾的絕對路徑+文件夾的名字

${workspaceRootFolderName} 當前打開的文件夾的名字

${file} 當前打開正在編輯的文件名,包括絕對路徑,文件名,文件後綴名

${relativeFile} 從當前打開的文件夾到當前打開的文件的路徑

${fileBasename} 當前打開的文件名+後綴名,不包括路徑

${fileBasenameNoExtension} 當前打開的文件的文件名,不包括路徑和後綴名

${fileDirname} 當前打開的文件所在的絕對路徑,不包括文件名

${fileExtname} 當前打開的文件的後綴名

${cwd} 啓動時的當前工做目錄

${lineNumber} 當前打開的文件,光標所在的行數

相關文章
相關標籤/搜索