使用Vscode 開發調試 C/C++ 項目

  1. 須要安裝的擴展 C/C++
    linux

    若是是遠程 Linux上開發還須要安裝 Remote Developmentshell

  1. 建立工做目錄後,代碼遠程克隆... 省略..json

  2. 建立項目配置文件,主要的做用是代碼智能提示,錯誤分析等等...
    按F1,輸入 C/C++ 選擇 編輯配置UI或者json 這個操做會生成 .vscode/c_cpp_properties.json 配置文件ui

修改相關的參數,如頭文件路徑,預約義參數,編譯器等spa

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**" // 包含了當前工做下全部頭文件,若是沒有在該目錄下須要引入
            ],
            "defines": [ // 這裏是項目中須要的預約義 LINUX環境一般須要加LINUX
                "LINUX",
                "ACI_TEST"
            ],
            "compilerPath": "/usr/bin/g++", // 編譯器程序
            "cStandard": "gnu99",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

(注意) 這個配置文件與編譯和執行沒有任何關係,它僅僅用於代碼提示和錯誤提示debug

  1. 代碼編譯和調試配置
    選中打開一個.c 或者 .cpp 文件,按F5 選擇 環境 和 編譯器

生成了2個文件3d

.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": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}", // 須要調試的程序路徑,如 ${workspaceFolder}/test
            "args": [], // 調試程序須要接收的參數,如 test -c =>  "args": ["-c"]
            "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++: g++ build active file", // 在調試以前須要作什麼, 就是當你按下f5, 首先會去不如編譯程序... make 或者 gcc ,g++ 編譯上面路徑的程序,
                                                             // "C/C++: g++ build active file" 只是一個名稱,它會去 tasks.json 中找這個名稱, 執行 這個任務以後開始調試
            "miDebuggerPath": "/usr/bin/gdb", // 調試的gdb ,要確保環境已經安裝了gdb, sudo apt install gdb
            "envFile": "${workspaceFolder}/.env" // 配置環境變量的文件路徑,下面會繼續說明用處.
        }
    ]
}

.vscode/tasks.jsoncode

{
    "tasks": [
        {
            "type": "cppbuild", // shell 類型...
            "label": "C/C++: g++ build active file", // preLaunchTask 的名稱
            "command": "/usr/bin/g++", // 編譯器
            "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"
}

這個文件就是怎麼去編譯你的程序,沒什麼可說 , make 或者 gcc g++ 編譯
貼一個個人項目 tasks 用make 編譯orm

{
    "tasks": [
        {
            "type": "shell",
            "label": "make_1",
            "command":"make -j6 -f makefile_local.mk 2>build_error.log",
            "options": {
                "cwd": "${workspaceFolder}/UnitTesting/"
            },
            "problemMatcher": []
        },
        {
            "type": "shell",
            "label": "make_2",
            "command":"make -j6 -f makefile_local.mk 2>build_error.log",
            "options": {
                "cwd": "${workspaceFolder}/"
            },
            "problemMatcher": []
        },
        {
            "type": "shell",
            "label": "build",
            "dependsOrder": "sequence",
            "dependsOn": [
                "make_2",
                "make_1"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": []
        }
    ],
    "version": "2.0.0"
}
  1. 環境變量配置, 上面launch.json 中配置了.env 文件的路徑

.env

LD_LIBRARY_PATH=./lib:/lib64

用途是: 若是個人程序須要依賴某個動態庫,好比,工做目錄下lib和lib64裏面是須要的動態庫,能夠在這裏加入環境變量,能夠是相對路徑,或者絕對路徑.
當前你也能夠在系統環境變量裏面設置

以上是 C/C++ 使用VScode的方法,若是對你有幫助,請點一個支持吧 _

相關文章
相關標籤/搜索