Visual Studio Code 配置C/C++環境(TDM-GCC)遇到的坑

安裝擴展(extension)

  • C/C++:又名 cpptools,提供Debug和Format功能
  • Code Runner:右鍵即可編譯運行單文件,但無法Dubug

 

第一個坑, 找不到gdb64.exe文件

下載TDM-GCC5.1後默認的安裝選項竟然不選gdb,這是什麼鬼,安裝說明文檔中有這麼一段話:

You'll need GDB particularly if you want to use an IDE with debugging support.

好吧,重新安裝,選中這個選項

另外安裝程序有將gcc加入到環境變量PATH的選項,這點倒不用我們操心了。

第二個坑,配置各種json文件

由於我更改默認安裝路徑到D:\TDM-GCC-64,需要更改幾個配置文件,先新建一個文件夾,比如F:\VS-Code-C,打開VScode,在其中打開這個文件夾,新建一個.vscode文件夾,在其中新建launch.json文件,拷貝內容如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",
            "type": "cppdbg",                         
            "request": "launch",           
            "targetArchitecture": "x86", 
            // 要進行調試的可執行文件的路徑               
            "program": "${workspaceRoot}/main", 
            // gdb調試器路徑,這裏選擇TDM-GCC安裝目錄下的gdb
            "miDebuggerPath":"D:/TDM-GCC-64/bin/gdb64.exe",
            "args": [],                            
            "stopAtEntry": false,                      
            "cwd": "${workspaceRoot}",             
            "externalConsole": true, 
            // 調試會話前執行的任務,這裏是task.json中配置好的編譯任務
            "preLaunchTask": "g++",
            // pretty-printing配置,可以直觀打印出C++中的容器
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        }
    ]
}

重點修改"miDebuggerPath"項,目錄層級用/隔開。

新建並配置tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 編譯命令參數
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
  }

第三個坑, c_cpp_properties.json的配置,

首先是"includePath"和"compilerPath" ,這個照例改爲自己GCC的安裝路徑

另外還要注意的兩點,配置不好會顯示兩個問題:

第一:IntelliSense模式msvc-x64與編譯器路徑不兼容

這是因爲msvc-x64對應的編譯器是Windows中的Visual Studio,咱們編譯器沒有選擇Visual Studio,所以IntelliSense 模式不兼容,此處,選擇和g++ on Mingw-w64兼容的IntelliSense 模式模式就可以了,此處選擇:gcc-x64,
————————————————
 

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "D:/TDM-GCC-64/include",
                "D:/TDM-GCC-64/x86_64-w64-mingw32/include",
                "D:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include",
                "D:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++"
            ],
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "intelliSenseMode": "gcc-x64",  //默認爲msvc-x64
            "compilerPath": "D:\\TDM-GCC-64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    //"clang_format": 在這裏設置, 現在先刪除
    "version": 4
}

 

第二:property clang_format is not allowed

目前還用不上,刪掉下面的設置即可

    "clang_format": {
        "style": "file",
        "fallback-style": "LLVM",
        "sort-includes": false
    },

下面新建一個main.cpp,在代碼界面點擊右鍵,選擇Run Code (前面安裝的擴展), 即可在終端中顯示結果。否則,必須在第七行下斷點,不然運行結果會一閃而過。

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello Vscode" << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章