VSCode 配置C、C++環境

VSCode中配置環境大概分如下幾步:c++

下載安裝VSCode
安裝 C/C++ for Visual Studio Code
安裝MinGW、配置系統環境變量
修改VSCode配置文件
首先打開VSCodejson

安裝微軟官方C/C++ for Visual Studio Code 
在VSCode界面中快捷鍵Ctrl+P 呼出輸入框輸入函數

ext install c
1
彈出擴展商店選擇第一個微軟官方插件 測試


使用MinGW安裝g++編譯 
確認好安裝目錄後continue到完成,打開後左側目錄選擇:spa

All Packages - MinGW - MinGW BaseSystem.net

mingw32-gcc-g++勾選。
mingw32-gdb必選,不然沒法調試 。
其中gcc和g++爲c和c++編譯器 。插件

選擇徹底部想要安裝的項後點擊左上角命令行

Installation - Apply Changes調試

因爲是國外的資源可能會是漫長的下載甚至失敗,萬般皆下品唯有把牆翻。code

配置系統環境變量Path

個人電腦 - 屬性 - 高級系統設置 - 環境變量 - Path

重啓VSCode 
用VSC新建一個文件夾後,再文件夾中建立一個c文件,輸入測試代碼:

#include <stdio.h> 
int main(int argc , char * args[]){
    printf("hello my world \n"); 
    return 0;
}
1
2
3
4
5
點擊左側調試按鈕,再點擊齒輪。

在彈出的選擇環境中選擇 C++(GDB)

而後會在工做目錄下的.vscode中生成一個launch.json的啓動配置文件,使用如下代碼參考或直接替換:

{    
    "version": "0.2.0",    
    "configurations": [    
        {    
            "name": "(gdb) Launch", // 配置名稱,將會在啓動配置的下拉菜單中顯示    
            "type": "cppdbg",       // 配置類型,這裏只能爲cppdbg    
            "request": "launch",    // 請求配置類型,能夠爲launch(啓動)或attach(附加)    
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",// 將要進行調試的程序的路徑    
            "args": [],             // 程序調試時傳遞給程序的命令行參數,通常設爲空便可    
            "stopAtEntry": false,   // 設爲true時程序將暫停在程序入口處,通常設置爲false    
            "cwd": "${workspaceRoot}", // 調試程序時的工做目錄,通常爲${workspaceRoot}即代碼所在目錄    
            "environment": [],    
            "externalConsole": true, // 調試時是否顯示控制檯窗口,通常設置爲true顯示控制檯    
            "MIMode": "gdb",    
            "miDebuggerPath": "E:\\MinGW\\bin\\gdb.exe", // miDebugger的路徑,注意這裏要與MinGw的路徑對應    
            "preLaunchTask": "g++", // 調試會話開始前執行的任務,通常爲編譯程序,c++爲g++, c爲gcc    
            "setupCommands": [    
                {     
                    "description": "Enable pretty-printing for gdb",    
                    "text": "-enable-pretty-printing",    
                    "ignoreFailures": true    
                }    
            ]    
        }    
    ]    
}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
特別注意替換launch.json中miDebuggerPath爲本身的MinGW目錄所對應的路徑。

替換後保存,而後切換至test.c,按F5進行調試,此時會彈出一個信息框要求你配置任務運行程序,點擊它,選擇最下面的Others。將彈出的tasks.json內容用下面的json替換

{       "version": "0.1.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           }       }   }   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 保存後,切換至test.c,再次按F5啓動調試,若是配置成功能夠看到界面一閃而過。

相關文章
相關標籤/搜索