Visual Studio Code 配置 mingw-w64的 GCC c + + 編譯器(g + +)和 GDB 調試器 Windows 教程


機器翻譯

官方鏈接:https://code.visualstudio.com/docs/cpp/config-mingw

Using GCC with MinGW

使用 GCC 和 MinGW

In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from mingw-w64 to create programs that run on Windows.

在本教程中,您將 Visual Studio Code 配置為使用來自 mingw-w64的 GCC c + + 編譯器(g + +)和 GDB 調試器來創建在 Windows 上運行的程序。

After configuring VS Code, you will compile and debug a simple Hello World program in VS Code. This tutorial does not teach you about GCC, GDB, Mingw-w64, or the C++ language. For those subjects, there are many good resources available on the Web.

在配置了 VS 代碼之后,您將用 VS 代碼編譯和調試一個簡單的 Hello World 程序。本教程不教授 GCC、 GDB、 mingw-w64或 c + + 語言。對於這些主題,網上有許多很好的資源。

If you have any problems, feel free to file an issue for this tutorial in the VS Code documentation repository.

如果您有任何問題,請隨意將本教程的問題歸檔到 VS 代碼文檔存儲庫中。

Prerequisites 先決條件#

To successfully complete this tutorial, you must do the following steps:

要成功完成本教程,您必須執行以下步驟:

  1. Install Visual Studio Code.

    安裝 Visual Studio 代碼。

  2. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X).

    為 VS 代碼安裝 c/c + + 擴展。您可以通過在 Extensions 視圖中搜索“ c + +”(Ctrl + Shift + x)來安裝 c/c + + 擴展。

    C/C++ extension

  3. Get the latest version of Mingw-w64 via MSYS2, which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries. Click here to download the MSYS2 installer. Then follow the instructions on the MSYS2 website to install Mingw-w64.

    通過 msys2獲得最新版本的 Mingw-w64,它提供了最新的 GCC、 mingw-w64和其他有用的 c + + 工具和庫。點擊這里下載 msys2安裝程序。然后按照 msys2網站上的說明安裝 Mingw-w64。

  4. Add the path to your Mingw-w64 bin folder to the Windows PATH environment variable by using the following steps:

    使用以下步驟將 Mingw-w64 bin 文件夾的路徑添加到 Windows PATH 環境變量文件夾中:

    1. In the Windows search bar, type 'settings' to open your Windows Settings. 在 Windows 搜索欄中,鍵入“設置”以打開 Windows 設置
    2. Search for 搜尋Edit environment variables for your account 為您的帳戶編輯環境變量.
    3. Choose the 選擇Path variable and then select 變量,然后選擇Edit 編輯.
    4. Select 選擇New 新 and add the Mingw-w64 destination folder path to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path: 並將 mingw-w64目標文件夾路徑添加到系統路徑。確切的路徑取決於您安裝了哪個版本的 Mingw-w64,以及安裝它的位置。如果你使用上面的設置來安裝 Mingw-w64,那么把它添加到路徑:C:\msys64\mingw64\bin.
    5. Select 選擇OK 好的 to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available. 保存更新后的 PATH。您需要重新打開任何控制台窗口,以便新的 PATH 位置可用

Check your MinGW installation 檢查您的 MinGW 安裝#

To check that your Mingw-w64 tools are correctly installed and available, open a new Command Prompt and type:

要檢查您的 mingw-w64工具是否正確安裝和可用,請打開一個新的命令提示符並鍵入:

g++ --version
gdb --version

If you don't see the expected output or g++ or gdb is not a recognized command, make sure your PATH entry matches the Mingw-w64 binary location where the compilers are located. If the compilers do not exist at that PATH entry, make sure you followed the instructions on the MSYS2 website to install Mingw-w64.

如果您沒有看到預期的輸出,或者 g + + 或 gdb 不是一個可識別的命令,請確保您的 PATH 條目與編譯器所在的 mingw-w64二進制位置匹配。如果 PATH 條目中不存在編譯器,請確保按照 msys2網站上的說明安裝 Mingw-w64。

Create Hello World 創建 Hello World#

From a Windows command prompt, create an empty folder called projects where you can place all your VS Code projects. Then create a sub-folder called helloworld, navigate into it, and open VS Code in that folder by entering the following commands:

在一個命令提示符文件夾中,創建一個名為項目的空文件夾,在這個文件夾中你可以放置所有的 VS 代碼項目。然后創建一個名為 helloworld 的子文件夾,導航到該文件夾,並通過輸入以下命令打開該文件夾中的 VS Code:

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

The "code ." command opens VS Code in the current working folder, which becomes your "workspace". As you go through the tutorial, you will see three files created in a .vscode folder in the workspace:

代碼。”命令在當前工作文件夾中打開 VS 代碼,該文件夾成為您的“工作區”。在閱讀本教程時,您將看到在。工作區中的 vscode 文件夾:

  • tasks.json (build instructions) (構建說明)
  • launch.json (debugger settings) (調試器設置)
  • c_cpp_properties.json (compiler path and IntelliSense settings) (編譯器路徑和 IntelliSense 設置)

Add a source code file 添加源代碼文件#

In the File Explorer title bar, select the New File button and name the file helloworld.cpp.

在 File Explorer 標題欄中,選擇 New File 按鈕並將文件命名為 helloworld.cpp。

New File title bar button

Add hello world source code 添加 hello world 源代碼#

Now paste in this source code:

現在粘貼下面的源代碼:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

Now press Ctrl+S to save the file. Notice how the file you just added appears in the File Explorer view (Ctrl+Shift+E) in the side bar of VS Code:

現在按 Ctrl + s 來保存文件。注意你剛剛添加的文件是如何出現在 VS Code 邊欄的 File Explorer 視圖(Ctrl + Shift + e)中的:

File Explorer

You can also enable Auto Save to automatically save your file changes, by checking Auto Save in the main File menu.

您也可以啟用自動保存,以自動保存您的文件更改,通過檢查自動保存在主文件菜單。

The Activity Bar on the far left lets you open different views such as Search, Source Control, and Run. You'll look at the Run view later in this tutorial. You can find out more about the other views in the VS Code User Interface documentation.

最左邊的活動欄允許您打開不同的視圖,如搜索、源代碼管理和運行。您將在本教程的后面看到 Run 視圖。您可以在 VS 代碼用戶界面文檔中找到關於其他視圖的更多信息。

Note: When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the X (Clear Notification).

注意: 當您保存或打開一個 c + + 文件時,您可能會看到來自 c/c + + 擴展的關於內部版本可用性的通知,它允許您測試新的特性和修復。可以通過選擇 x (清除通知)來忽略此通知。

Explore IntelliSense 探索智能感知#

In your new helloworld.cpp file, hover over vector or string to see type information. After the declaration of the msg variable, start typing msg. as you would when calling a member function. You should immediately see a completion list that shows all the member functions, and a window that shows the type information for the msg object:

在新的 helloworld.cpp 文件中,將鼠標懸停在 vector 或 string 上以查看類型信息。在聲明 msg 變量之后,開始鍵入 msg。就像調用成員函數一樣。您應該立即看到一個完成列表,顯示所有成員函數,以及一個窗口,顯示 msg 對象的類型信息:

Statement completion IntelliSense

You can press the Tab key to insert the selected member; then, when you add the opening parenthesis, you will see information about any arguments that the function requires.

您可以按 Tab 鍵插入選定的成員; 然后,當您添加開始括號時,您將看到有關函數所需的任何參數的信息。

Build helloworld.cpp#

Next, you'll create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable file based on the source code.

接下來,您將創建一個 tasks.json 文件來告訴 VS 代碼如何構建(編譯)程序。此任務將調用 g + + 編譯器,以基於源代碼創建可執行文件。

From the main menu, choose Terminal > Configure Default Build Task. In the dropdown, which will display a tasks dropdown listing various predefined build tasks for C++ compilers. Choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.

從主菜單中,選擇終端 > 配置默認生成任務。在下拉列表中,它將顯示一個任務下拉列表,列出 c + + 編譯器的各種預定義的構建任務。選擇 g + + 。Exe 生成活動文件,該文件將生成當前在編輯器中顯示(活動)的文件。

Tasks C++ build dropdown

This will create a tasks.json file in a .vscode folder and open it in the editor.

這將在.vscode 文件夾中創建 tasks.json 文件,並在編輯器中打開它。

Your new tasks.json file should look similar to the JSON below:

您的新 tasks.JSON 文件看起來應該類似於下面的 JSON:

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe build active file",
      "command": "C:/msys64/mingw64/bin/g++.exe",
      "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "compiler: C:/msys64/mingw64/bin/g++.exe"
    }
  ],
  "version": "2.0.0"
}

The command setting specifies the program to run; in this case that is g++. The args array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler. This task tells g++ to take the active file (${file}), compile it, and create an executable file in the current directory (${fileDirname}) with the same name as the active file but with the .exe extension (${fileBasenameNoExtension}.exe), resulting in helloworld.exe for our example.

命令設置指定要運行的程序; 在本例中是 g + + 。Args 數組指定將傳遞給 g + + 的命令行參數。這些參數必須按編譯器期望的順序指定。這個任務告訴 g + + 獲取活動文件(\({ file }) ,對其進行編譯,並在工作目錄文件中創建一個與活動文件同名的可執行文件(\){ fileDirname })。Exe 擴展(${ fileBasenameNoExtension })。Exe) ,導致 helloworld.exe 為我們的示例。

Note: You can learn more about tasks.json variables in the variables reference.

注意: 您可以在變量引用中了解更多關於 tasks.json 變量的信息。

The label value is what you will see in the tasks list; you can name this whatever you like.

標簽值就是您將在任務列表中看到的內容; 您可以隨心所欲地命名它。

The "isDefault": true value in the group object specifies that this task will be run when you press Ctrl+Shift+B. This property is for convenience only; if you set it to false, you can still run it from the Terminal menu with Tasks: Run Build Task.

Group 對象中的“ isDefault”: true value 指定當您按下 Ctrl + Shift + b 時將運行此任務。此屬性僅為方便起見; 如果將其設置為 false,仍然可以在終端菜單中使用 Tasks: Run Build Task 運行它。

Running the build 運行構建#

  1. Go back to helloworld.cpp. Your task builds the active file and you want to build helloworld.cpp.

    返回到 helloworld.cpp,您的任務構建活動文件,並希望構建 helloworld.cpp。

  2. To run the build task defined in tasks.json, press Ctrl+Shift+B or from the Terminal main menu choose Run Build Task.

    要運行 tasks. json 中定義的構建任務,請按 Ctrl + Shift + b 或從終端主菜單中選擇 Run Build Task。

  3. When the task starts, you should see the Integrated Terminal panel appear below the source code editor. After the task completes, the terminal shows output from the compiler that indicates whether the build succeeded or failed. For a successful g++ build, the output looks something like this:

    當任務啟動時,您應該會看到集成終端面板出現在源代碼編輯器下方。任務完成后,終端顯示編譯器的輸出,指示生成是成功還是失敗。對於一個成功的 g + + 構建,輸出如下:

    G++ build output in terminal

  4. Create a new terminal using the + button and you'll have a new terminal with the helloworld folder as the working directory. Run dir and you should now see the executable helloworld.exe.

    創建一個新的終端使用 + 按鈕,你將有一個新的終端與 helloworld 文件夾作為工作目錄。運行 dir,現在應該會看到可執行的 helloworld.exe。

    Hello World in PowerShell terminal

  5. You can run helloworld in the terminal by typing helloworld.exe (or .\helloworld.exe if you use a PowerShell terminal).

    可以通過鍵入 helloworld.exe (如果使用 PowerShell 終端,則可以鍵入.helloworld.exe)在終端中運行 helloworld。

Note: You might need to press Enter a couple of times initially to see the PowerShell prompt in the terminal. This issue should be fixed in a future release of Windows.

注意: 一開始您可能需要按回車幾次,才能在終端中看到 PowerShell 提示符。這個問題應該在未來的 Windows 版本中得到解決。

Modifying tasks.json#

You can modify your tasks.json to build multiple C++ files by using an argument like "${workspaceFolder}\\*.cpp" instead of ${file}. This will build all .cpp files in your current folder. You can also modify the output filename by replacing "${fileDirname}\\${fileBasenameNoExtension}.exe" with a hard-coded filename (for example "${workspaceFolder}\\myProgram.exe").

您可以使用“ ${ worksomatoolder } *”這樣的參數修改 tasks.json 以構建多個 c + + 文件。而不是 ${ file }。這將建立所有。當前文件夾中的 cpp 文件。還可以通過替換“ ${ fileDirname } ${ fileBasenameNoExtension }來修改輸出文件名。Exe 文件名(例如“ ${ worksomatoolder } myProgram.exe.exe”)。

Debug helloworld.cpp#

Next, you'll create a launch.json file to configure VS Code to launch the GDB debugger when you press F5 to debug the program.

接下來,您將創建一個 launch.json 文件來配置 VS Code,以便在按 f5調試程序時啟動 GDB 調試器。

  1. From the main menu, choose 從主菜單中選擇Run 快跑 > Add Configuration... 添加配置.. and then choose 然后選擇C++ (GDB/LLDB) C + + (GDB/ldb).
  2. You'll then see a dropdown for various predefined debugging configurations. Choose 然后您將看到各種預定義調試配置的下拉列表g++.exe build and debug active file.

C++ debug configuration dropdown

VS Code creates a launch.json file, opens it in the editor, and builds and runs 'helloworld'.

VS Code 創建一個 launch.json 文件,在編輯器中打開它,構建並運行“ helloworld”。

{
  "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": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: g++.exe build active file"
    }
  ]
}

The program setting specifies the program you want to debug. Here it is set to the active file folder ${fileDirname} and active filename with the .exe extension ${fileBasenameNoExtension}.exe, which if helloworld.cpp is the active file will be helloworld.exe.

程序設置指定要調試的程序。在這里,它被設置為活動文件夾 ${ fileDirname }和帶有。擴展名 ${ fileBasenameNoExtension }。Exe,如果 helloworld.cpp 是活動文件,則該文件將為 helloworld.exe。

By default, the C++ extension won't add any breakpoints to your source code and the stopAtEntry value is set to false.

默認情況下,c + + 擴展不會向源代碼添加任何斷點,而 stopAtEntry 值設置為 false。

Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.

將 stopAtEntry 值更改為 true,以便在開始調試時使調試器在 main 方法上停止。

Note: The preLaunchTask setting is used to specify task to be executed before launch. Make sure it is consistent with the tasks.json file label setting.

注意: preLaunchTask 設置用於指定啟動前要執行的任務。確保它與 tasks.json 文件標簽設置一致。

Start a debugging session 啟動調試會話#

  1. Go back to 回到helloworld.cpp so that it is the active file. 所以它就是活動文件
  2. Press 新聞稿F5 or from the main menu choose 或者從主菜單中選擇Run > Start Debugging 運行 > 開始調試. Before you start stepping through the source code, let's take a moment to notice several changes in the user interface: .在你開始介紹源代碼之前,讓我們花點時間注意一下用戶界面的幾個變化:
  • The Integrated Terminal appears at the bottom of the source code editor. In the Debug Output tab, you see output that indicates the debugger is up and running.

    集成終端顯示在源代碼編輯器的底部。在“調試輸出”選項卡中,可以看到指示調試器已啟動並正在運行的輸出。

  • The editor highlights the first statement in the main method. This is a breakpoint that the C++ extension automatically sets for you:

    編輯器突出顯示 main 方法中的第一條語句。這個斷點是 c + + 擴展自動為你設置的:

    Initial breakpoint

  • The Run view on the left shows debugging information. You'll see an example later in the tutorial.

    左邊的 Run 視圖顯示了調試信息。

  • At the top of the code editor, a debugging control panel appears. You can move this around the screen by grabbing the dots on the left side.

    在代碼編輯器的頂部,將出現一個調試控制面板。你可以通過抓取屏幕左邊的點來移動它。

Step through the code 逐步完成代碼#

Now you're ready to start stepping through the code.

現在您已經准備好開始逐步執行代碼了。

  1. Click or press the Step over icon in the debugging control panel.

    單擊或按下調試控制面板中的 Step over 圖標。

    Step over button

    This will advance program execution to the first line of the for loop, and skip over all the internal function calls within the vector and string classes that are invoked when the msg variable is created and initialized. Notice the change in the Variables window on the left.

    這將把程序執行提前到 for 循環的第一行,並跳過創建和初始化 msg 變量時在 vector 和 string 類中調用的所有內部函數調用。注意左側 Variables 窗口中的變化。

    Debugging windows

    In this case, the errors are expected because, although the variable names for the loop are now visible to the debugger, the statement has not executed yet, so there is nothing to read at this point. The contents of msg are visible, however, because that statement has completed.

    在這種情況下,預計會出現錯誤,因為盡管調試器現在可以看到循環的變量名,但語句尚未執行,因此此時沒有什么可讀的內容。但是,msg 的內容是可見的,因為該語句已經完成。

  2. Press Step over again to advance to the next statement in this program (skipping over all the internal code that is executed to initialize the loop). Now, the Variables window shows information about the loop variables.

    再次按“步驟”以前進到此程序中的下一個語句(跳過為初始化循環而執行的所有內部代碼)。現在,Variables 窗口顯示有關循環變量的信息。

  3. Press Step over again to execute the cout statement. (Note that as of the March 2019 release, the C++ extension does not print any output to the Debug Console until the loop exits.)

    再次按“步驟”以執行 cout 語句。(請注意,在2019年3月的版本中,c + + 擴展在循環退出之前不會將任何輸出打印到 Debug 控制台。)

  4. If you like, you can keep pressing Step over until all the words in the vector have been printed to the console. But if you are curious, try pressing the Step Into button to step through source code in the C++ standard library!

    如果願意,可以繼續按下 Step,直到向量中的所有單詞都打印到控制台。但是如果你好奇的話,可以試着按下 Step Into 按鈕來逐步查看 C++標准程式庫中的源代碼!

    Breakpoint in gcc standard library header

    To return to your own code, one way is to keep pressing Step over. Another way is to set a breakpoint in your code by switching to the helloworld.cpp tab in the code editor, putting the insertion point somewhere on the cout statement inside the loop, and pressing F9. A red dot appears in the gutter on the left to indicate that a breakpoint has been set on this line.

    要返回到自己的代碼,一種方法是繼續按下“步驟”。另一種方法是在代碼中設置斷點,方法是切換到代碼編輯器中的 helloworld.cpp 選項卡,將插入點放在循環中 cout 語句的某個位置,然后按 F9。左邊的 gutter 中出現一個紅點,表示在此行上設置了斷點。

    Breakpoint in main

    Then press F5 to start execution from the current line in the standard library header. Execution will break on cout. If you like, you can press F9 again to toggle off the breakpoint.

    然后按 f5從標准庫頭中的當前行開始執行。執行將在結束時終止。如果願意,可以再次按 f9以關閉斷點。

    When the loop has completed, you can see the output in the Integrated Terminal, along with some other diagnostic information that is output by GDB.

    循環完成后,您可以看到集成終端中的輸出,以及 GDB 輸出的其他診斷信息。

    Debug output in terminal

Set a watch 設置一個手表#

Sometimes you might want to keep track of the value of a variable as your program executes. You can do this by setting a watch on the variable.

有時您可能希望在程序執行時跟蹤變量的值。你可以通過在變量上設置一個表來實現這一點。

  1. Place the insertion point inside the loop. In the Watch window, click the plus sign and in the text box, type word, which is the name of the loop variable. Now view the Watch window as you step through the loop.

    將插入點放在循環中。在“監視”窗口中,單擊加號,並在文本框中鍵入 word,這是循環變量的名稱。現在在循環中單步執行時查看“監視”窗口。

    Watch window

  2. Add another watch by adding this statement before the loop: int i = 0;. Then, inside the loop, add this statement: ++i;. Now add a watch for i as you did in the previous step.

    通過在循環之前添加以下語句來添加另一個 watch: int i = 0; 。然后,在循環中,添加以下語句: + + i; 。現在為 i 添加一個手表,如上一步所做的那樣。

  3. To quickly view the value of any variable while execution is paused on a breakpoint, you can hover over it with the mouse pointer.

    若要在斷點上暫停執行時快速查看任何變量的值,可以使用鼠標指針將鼠標懸停在該變量上。

    Mouse hover

C/C++ configurations C/c + + 配置#

If you want more control over the C/C++ extension, you can create a c_cpp_properties.json file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.

如果您希望對 c/c + + 擴展有更多的控制權,可以創建一個 c _ cpp _ properties。Json 文件,它允許您更改設置,例如編譯器的路徑,include 路徑,c + + 標准(默認是 c + + 17) ,等等。

You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P).

您可以通過運行命令 c/c + + : Edit Configurations (UI)從 Command Palette (Ctrl + Shift + p)查看 c/c + + configuration UI。

Command Palette

This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called c_cpp_properties.json in the .vscode folder.

這將打開 c/c + + Configurations 頁面。當您在這里進行更改時,VS Code 將它們寫入一個名為 c_cpp _ properties 的文件中。傑森在。Vscode 文件夾。

Here, we've changed the Configuration name to GCC, set the Compiler path dropdown to the g++ compiler, and the IntelliSense mode to match the compiler (gcc-x64)

這里,我們將 Configuration 名稱更改為 GCC,將 Compiler 路徑下拉到 g + + 編譯器,將 IntelliSense 模式設置為與編譯器匹配(GCC-x64)

Command Palette

Visual Studio Code places these settings in .vscode\c_cpp_properties.json. If you open that file directly, it should look something like this:

Visual Studio Code 將這些設置放在. vscode c _ cpp _ properties. json 中,如果直接打開該文件,它應該是這樣的:

{
  "configurations": [
    {
      "name": "GCC",
      "includePath": ["${workspaceFolder}/**"],
      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
      "windowsSdkVersion": "10.0.18362.0",
      "compilerPath": "C:/msys64/mingw64/bin/g++.exe",
      "cStandard": "c17",
      "cppStandard": "c++17",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ],
  "version": 4
}

You only need to add to the Include path array setting if your program includes header files that are not in your workspace or in the standard library path.

如果程序包含不在工作區或標准庫路徑中的頭文件,則只需添加到 Include 路徑數組設置。

Compiler path 編譯器路徑#

The extension uses the compilerPath setting to infer the path to the C++ standard library header files. When the extension knows where to find those files, it can provide features like smart completions and Go to Definition navigation.

擴展名使用 compilerPath 設置來推斷 C++標准程式庫文件的路徑。當擴展知道在哪里可以找到這些文件時,它可以提供智能完成和定義導航等功能。

The C/C++ extension attempts to populate compilerPath with the default compiler location based on what it finds on your system. The extension looks in several common compiler locations.

C/c + + 擴展試圖根據它在系統中發現的內容,用默認的編譯器位置填充 compilerPath。該擴展查找幾個常見的編譯器位置。

The compilerPath search order is:

compilerPath 搜索順序是:

  • First check for the Microsoft Visual C++ compiler 首先檢查 Microsoft Visual c + + 編譯器
  • Then look for g++ on Windows Subsystem for Linux (WSL) 然后在 Linux (WSL)的 Windows 子系統中查找 g + +
  • Then g++ for Mingw-w64. 然后用 g + + 表示 Mingw-w64

If you have Visual Studio or WSL installed, you may need to change compilerPath to match the preferred compiler for your project. For example, if you installed Mingw-w64 version 8.1.0 using the i686 architecture, Win32 threading, and sjlj exception handling install options, the path would look like this: C:\Program Files (x86)\mingw-w64\i686-8.1.0-win32-sjlj-rt_v6-rev0\mingw64\bin\g++.exe.

如果安裝了 visualstudio 或 WSL,則可能需要更改 compilerPath 以匹配項目的首選編譯器。例如,如果您使用 i686架構、 win32線程和 sjlj 異常處理安裝選項安裝 Mingw-w64 version 8.1.0,那么路徑如下: c: Program Files (x86) Mingw-w64 i686-8.1.0-Win32-sjlj-rt _ v6-rev0 mingw64 bin g + + 。執行。

Troubleshooting 故障排除#

MSYS2 is installed, but g++ and gdb are still not found 已經安裝了 MSYS2,但是仍然沒有找到 g + + 和 gdb#

You must follow the steps on the MSYS2 website and use the MSYS CLI to install Mingw-w64, which contains those tools.

您必須遵循 msys2網站上的步驟,並使用 MSYS CLI 安裝 Mingw-w64,其中包含這些工具。


免責聲明!

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



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