vscode+cpp+mingw64環境配置


參考:https://code.visualstudio.com/docs/cpp/config-mingw

前提條件:

進行環境配置之前請先做好以下工作

1、安裝vscode

2、安裝 c++ extension for VS Code (https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

3、安裝mingw-w64。注意安裝路徑不能有空格。比如可以安裝到 d:\Mingw-w64

4、將Minge-w64的bin文件夾路徑添加到windows的PATH環境變量里。比如我的路徑是 c:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin

在vscode中創建工作區

方式1:點擊菜單“文件”->“打開文件夾”,選擇一個文件夾(比如project1),則文件夾project1就變成當前工作區。

方式2:

在命令行執行以下命令

 

mkdir helloworld
cd helloworld
code .

 

hellowork目錄就成了當前工作區。  

 

配置完成后會在工作區文件夾生成下面的文件

 

  • c_cpp_properties.json (compiler path and IntelliSense settings)(編譯器路徑和智能感知設置)
  • tasks.json (build instructions)(生成說明)
  • launch.json (debugger settings)(調試設置)

設置編譯器路徑

按 Ctrl+Shift+P,打開命令面板

在彈出的窗口中輸入 C/C++,在彈出的列表中選擇“Edit Configurations (UI)”,然后系統會打開C/C++ IntelliSense Configurations 頁面。在此頁面所做的修改會被保存到文件c_cpp_properties.json

將編譯器路徑修改為上面安裝的mingw-w64的路徑(D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe)

將IntelliSense 模式修改為 gcc-x64

我配置出來的文件是這樣的

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "D:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "compilerArgs": [],
            "browse": {
                "path": [
                    "${workspaceFolder}/**"
                ],
                "limitSymbolsToIncludedHeaders": true
            }
        }
        
    ],
    "version": 4
}
.vscode/c_cpp_properties.json

創建生成任務

本小節創建一個tasks.json文件,告訴vscode如何生成程序。生成任務會調用g++程序將源碼編譯成可執行文件。

1、按下 Ctrl+Shift+P 打開命令面板--》輸入 "task"--》選擇 Tasks: Configure Default Build Task.--》在列表中選擇 從模板創建 tasks.json文件,--》選擇 Others. VS Code 會創建一個最小的tasks.json 文件並且打開。

用下面的內容替代文件的內容

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "build",
        "type": "shell",
        "command": "g++",
        "args": [ "-g",
                            "${file}",
                           "-o",
                           "${fileDirname}\\${fileBasenameNoExtension}.exe"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }
tasks.json

 

配置調試設置

本小節的設置是為了當用戶按下F5時讓VSCode啟動gcc調試器

在命令面板輸入 "launch" ,軟后選擇 Debug: Open launch.json. 然后選擇 the GDB/LLDB,然后選擇“g++.exe build and debug active file”.

生成后的文件如下

{
    // 使用 IntelliSense 了解相關屬性。 
    // 懸停以查看現有屬性的描述。
    // 欲了解更多信息,請訪問: 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": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}
launch.json

 

 

配置完畢


免責聲明!

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



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