VS code 配置C++環境(launch.json tasks.json c_cpp_properties.json文件)

vs code 配置C++過程中竟然遇到了點小問題,所以記錄一下。

vs code 官網下載:https://code.visualstudio.com/。

mingw-w64編譯器下載 http://mingw-w64.org/doku.php/download,安裝過程中需要根據電腦架構Architecture(32位還是64位)做一個選擇:

32位選i686,64位選擇x86_64選項,其餘不變,MinGW-W64安裝完成後將bin路徑添加到環境變量。在命令行中或windowpowershell 中檢查安裝情況: gcc -v ,gdb -v ,檢查安裝成功。設置環境變量的時候可以直接將C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0路徑下的bin文件夾拷貝到C:\mingw64,將路徑C:\mingw64\bin添加到系統的環境變量Path.

想知道MinGW是啥各個選項是什麼,看這裏(就是篇幅有點長):https://www.cnblogs.com/ggg-327931457/p/9694516.html。

vs code配置C++官網教程: https://code.visualstudio.com/docs/cpp/config-mingw。配置過程講解的非常詳細全面,注意一定要在整篇教程讀完之後再去操作,仔細看基本上都可以看懂。

這篇文章的重點是:在配置過程中的tasks.json(編譯指令設置)、launch.json(調試器設置)兩個文件是自動生成 的,可能有時候會改動其中的g++ 和 gdb 路徑。

完成C++的.cpp 文件之後:選擇左側調試器 Run and Debug ->create a lauch.json file。

          接下來,選擇C++ (GDB/LLDB)這個選項。如果沒有出現這個選項,將上一步生成的launch.json文件刪掉,重複上述操作。

          配置好的launch.json如下圖,注意“mDebugerPath”是否正確:

{
    // 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++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "爲 gdb 啓用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}

然後,再選擇g++.exe build and debug active file選項。gcc.exe 選項是編譯C語言的。

配置好的tasks.json 如下圖,注意“command” “cwd”路徑。

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw64\\bin"
            }
        }
    ],
    "version": "2.0.0"
}

這樣tasks.json、launch.json兩個文件自動添加完成。

至於c_cpp_properties.json 文件的作用和試用方法,在官方文檔https://code.visualstudio.com/docs/cpp/config-mingw尾部有詳細講解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章