VSCode official documentation-Chinese translation-Configuring C/C++ language programming environment

Official documentation

The following is the link to the official documentation:
Official documentation
Please correct me if there are any errors in the translation.

C++

GCC on Windows

GCC using MinGW

In this tutorial, you will configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB from mingw-w64 Debugger to create programs that run on Windows. After configuring VS Code, you will compile, run, and debug a simple Hello World program.

This tutorial will not teach you about GCC, GDB, MinGW-w64, or the C++ language. There are many good resources available online for learning about these topics.

If you encounter any problems, you can always raise an issue inVS Code Documentation Repository.

Prerequisites

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

  1. Anso Visual Studio Code.
  2. Install C/C++ extension for VS Code. You can install C/C++ extensions by searching for "C++" (Ctrl+Shift+X) in the extensions view.
    Please add image description

Installing the MinGW-w64 toolchain

Get the latest version of MinGW-w64 throughMSYS2. MSYS2 provides the latest GCC, MinGW-w64 and other useful C++ tools and A local build of the library. This will provide you with the necessary tools to compile, debug, and configure your code for use with IntelliSense.

  1. You can download the latest installer from the MSYS2 page, or use thisdirect link to download.

  2. Run the installer and follow the steps of the installation wizard. Please note that MSYS2 requires 64-bit Windows 8.1 or newer.

  3. In the wizard, select your desired installation directory. Make a note of this directory as you will need it later. Recommended directories are acceptable in most cases. The same goes for the steps to set up a Start menu shortcut. Once the installation is complete, make sure the Run MSYS2 now checkbox is selected and select Finish ". This will open an MSYS2 terminal window for you.

  4. In this terminal, install the MinGW-w64 toolchain by running the following command:

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
  1. Accept the defaultnumber of packages in the group by pressing theEnter key. 工具链
    Please add image description

  2. When prompted whether to continue the installation, enter Y.

  3. Add the path to the MinGW-w64 bin folder to the Windows PATH environment variables by following these steps:

    1. In the Windows search bar, typeSettings to open Windows Settings.
    2. SearchEdit the account's environment variables.
    3. In yourUser Variables, selectPathVariables, then selectEdit.
    4. SelectNew to add the MinGW-w64 target folder recorded during the installation to the list. If you used the default settings above, the path would be: C:\msys64\ucrt64\bin.
    5. SelectOK to save the updated PATH. You will need to reopen any console windows to use the new PATH location.
Check your MinGW installation

To check that your MinGW-w64 tools are installed correctly and available, open a new command prompt window and enter: a>

gcc --version
g++ --version
gdb --version

You should see output showing the versions of GCC, g++, and GDB you have installed. If this were not the case:

  1. Make sure the entry in your PATH variable matches the binary location of the MinGW-w64 installed toolchain. If the compiler does not exist in that PATH entry, make sure you follow the previous instructions.
  2. Ifgcc has the correct output but gdb does not, then you need to install your missing package from the MinGW-w64 toolset.
    • If the error message "The value of miDebuggerPath is invalid." appears during compilation, one reason may be that you are missing themingw-w64-gdb package.

Create a Hello World app (Create a Hello World app)

First, let's set up a project.

  1. Open Windows Command Prompt (typeCommand Prompt in the Windows search bar), then
  2. Run the following command. These commands will create an empty folder named projects where you can place all your VS Code projects. Next, use the following command to create and enter a subfolder named helloworld. From there, you'll openhelloworld directly in VS Code.
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

The "code ." command will open VS Code in the current working folder, which will become your "workspace". Since this is a folder you created, you can select Yes, I trust this author to accept the workspace trust Dialog box.

As you work through the tutorial, you will see three files created in the .vscode folder of your workspace:

  • tasks.json(Build Instructions)
  • launch.json(Debugger settings)
  • c_cpp_properties.json(Compiler path and IntelliSense settings)
Add a source code file

In the File Explorer title bar, select theNew File button and name the filehelloworld.cpp.

Please add image description

Add the source code of helloworld.

Now paste the following source code in:

#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 pressCtrl+S to save the file. Note that in the File Explorer view (Ctrl+Shift+E) of the VS Code sidebar, the file you just added is How it is displayed.
Please add image description
You can also choose to enable AutoSave to automatically save file changes by selecting File> AutoSave. You can learn more about other views in VS CodeUser Interface Documentation.

Note: When you save or open a C++ file, you may receive a notification from the C/C++ extension that an Insiders version is available that can Lets you test new features and fixes. You can ignore this notification by selecting X(Clear notification).

探索IntelliSense(Explore IntelliSense)

IntelliSense is a tool that helps you write code faster and more efficiently by adding code editing features such as code completion, parameter information, quick information, and member lists.

To see IntelliSense in action, hover over vector or string to see their type information. If you type msg. on line 10, you can see a list of recommended function call completions, all generated by IntelliSense:
Please add image description
You can Press the Tab key to insert the selected member. If opening brackets are subsequently added, IntelliSense will display the required parameter information.

If IntelliSense has not been configured yet, open the Command Palette (Ctrl+Shift+P) and enter to select IntelliSense configuration. From the compiler's drop-down list, select使用gcc.exe to configure.

Run helloworld.cpp (Run helloworld.cpp)

Remember that C++ extensions use the C++ compiler you have installed on your computer to build your program. Before trying to run and debug in VS Code, make sure you have completed the "Install the MinGW-w64 toolchain" step. helloworld.cpp

  1. Openhelloworld.cpp to make it the active file.

  2. Click the Run button in the upper right corner of the editor.
    Please add image description

  3. Select the C/C++: g++.exe build and debug activity file from the list of compilers detected by the system.
    Please add image description

You will only be asked to select a compiler on the first runhelloworld.cpp. This compiler will be set as the "default" compiler in the tasks.json file.

  1. After a successful build, your program output will be displayed in the integrated terminal.
    Please add image description
    Congratulations! You just ran your first C++ program in VS Code!
Understanding tasks.json

When you run your program for the first time, the C++ extension creates a tasks.json file, which you can find in the project's .vscode folder it. tasks.jsonStores your build configuration.

Your newtasks.json file should resemble the following JSON:

{
    
    
  "tasks": [
    {
    
    
      "type": "cppbuild",
      "label": "C/C++: g++.exe build active file",
      "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
    
    
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
    
    
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

Note: You can learn more aboutVariable Reference =4>Variable information. tasks.json

commandThe setting specifies the program to run; in this case g++. argsArray specifying the command line arguments to be passed to g++. The parameters are listed in this file in the specific order expected by the compiler.

This task tells g++ to get the currently open file (${file}), compile it, and create a file in the current directory (${fileDirname}) equal to the currently open file. An executable file with the same name but with the extension .exe (${fileBasenameNoExtension}.exe). For us this will yieldhelloworld.exe.

labelThe value is the name you will see in the task list; you can name it whatever you want.

detailThe value is the task description you will see in the task list. It is strongly recommended to rename this value to distinguish it from similar tasks.

From now on, the Run button will read information fromtasks.json to determine how to build and run your program. You can define multiple build tasks in tasks.json, and the task marked as default will become the task used when the run button is clicked. If you need to change the default build task, you can run the Tasks: Configure Default Build Task command in the command panel. Alternatively, you can modify thetasks.json file and remove the default settings by replacing the following sections:

    "group": {
    
    
        "kind": "build",
        "isDefault": true
    },

Replace with:

    "group": "build",

7/14/2023

weekly updates

Very good, you can try it next:Official documentation

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/133810731