Visual Studio Code (vscode)編譯C++


Visual Studio Code (簡稱 VS Code / VSC) 是一款免費開源的現代化輕量級代碼編輯器,支持幾乎所有主流的開發語言的語法高亮、智能代碼補全、自定義熱鍵、括號匹配、代碼片段、代碼對比 Diff、GIT 等特性,可謂是微軟的良心之作。

下載安裝VS Code

點擊下載鏈接,選擇合適的安裝程序

安裝cpptools插件

打開VS Code,快捷鍵ctrl+shift+p呼出命令框,輸入以下命令

ext install cpptools

稍等片刻會出現插件安裝列表,如圖:

點擊箭頭所指處的按鈕安裝插件,安裝過程可能會有些慢耐心等待 ,安裝完成后vscode會提示你重啟vscode。

安裝MINGW-W64

這里我建議安裝mingw-w64,比較穩定。
在windows下安裝完成后需要設置環境變量

c:\mingw-w64\bin\

配置調試環境

1.文件-->打開文件夾,設置項目路徑

2.新建一個.vscode文件夾

3.創建一個launch.json啟動配置文件

{
   "version": "0.2.0",
   "configurations": [
       {
           "name": "C++ Launch (GDB)",                
           "type": "cppdbg",                         
           "request": "launch",                        
           "targetArchitecture": "x86",                
           "program": "${workspaceRoot}\\${fileBasename}.exe",                 
           "miDebuggerPath":"C:\\mingw-w64\\bin\\gdb.exe", 
           "args": [],     
           "stopAtEntry": false,                  
           "cwd": "${workspaceRoot}",                  
           "externalConsole": true,                  
           "preLaunchTask": "g++"                    
           }
   ]
}

4.創建一個tasks.json的配置文件

{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","-std=c++11","${file}","-o","${workspaceRoot}\\${fileBasename}.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
        }
    }
}

5.創建一個c_cpp_properties.json文件,注意不同的版本的MINGW路徑可能有些許不同

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/tr1",
                "C:/mingw-w64/x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "path": [
                    "${workspaceRoot}",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
                    "C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/tr1",
                    "C:/mingw-w64/x86_64-w64-mingw32/include"
                ]
            },
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        }
    ],
    "version": 3
}

6.新建一個test.cpp文件,並點擊左側的調試按鈕


    #include <iostream>
    using namespace std;

    int main()
    {
        cout<<"Hello World!"<<endl;
        return 0;
    }

運行結果

為了看到輸出效果,我們可以在test.cpp的最后一行上設置一個斷點。運行效果如下:

更新:

  1. 2017-12-20 MINGW-W64替代了之前的MINGW
  2. 2018-02-13 修復 了includePath,避免了提示cannot open source file "xxx.h"

參考資料

  1. C/C++ for VS Code
  2. c_cpp_properties.json template


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM