Vscode configures C/C++ environment (super detailed nanny level teaching)

        In the first semester of my freshman year, I was given vscode by my senior, but I couldn’t configure it after downloading and installing it. After seven or eight hours of searching and asking, I finally configured it. Many students around me don’t know how to configure, so they all come to me for help. In addition, when I was groping for configuration, I felt that there were no detailed and reliable tutorials on the Internet, so I decided to record the configuration process for your reference. I hope it can help To the students who want to use it like me but don't know how to configure vscode.

Table of contents

The first step is to download and install VSCode

The second step is to download and install g++

The third step is to install the VSCode plug-in

The fourth step is to configure the debugging function

something else

1. Chinese display garbled characters

         2. Run in terminal

3. When debugging, it shows "cannot find g++"


 

The first step is to download and install VSCode

This should be the easiest step, I believe you can do it yourself. If you feel that the download is particularly slow on the vscode official website, you can try the Tencent Software Center. I downloaded it from this webpage. After downloading, follow the prompts to install it.
5142db7b754a4b888344424669d9a2e8.png

 The second step is to download and install g++

        MinGW Distro - nuwen.net https://nuwen.net/mingw.html

This is the link I found in the comment area when I was looking for tutorials at station b. You can download it with your own network 

After entering, click the link circled in the picture to download

8bf478d19442443290f89282f26b20aa.png

Install after the download is complete, choose the installation path yourself, you need to remember the installation path, it will be used soon

Note that Chinese cannot appear in the path! Note that Chinese cannot appear in the path! Note that Chinese cannot appear in the path!

370839d8d63643578a263d14947fe922.png

For convenience, I installed it directly on the C drive.

c7a8c2bf98824666aafc0b613e49a381.png

After the installation is complete, we open the installation path just now, find and open MinGW -> bin, enter the bin folder and click here, right click to copy the path

bb1d4d8f4d9148ca96f122015990f920.png

 Then we enter the settings, search for "environment variables", select "edit system environment variables"

742c0d9349fa45afbb020e0010db95e7.png

Click to enter the environment variable 

f5f83baaa7b9432497832867cf9c718b.png

 Double-click Path

1d3dc3bd218d4580b368a8dd893ff501.png

 Click New and paste the path you just copieda8c489d5d28b48ba84aefb64fd23de11.png

 Then click OK three times in a row.

 Then let's take a look at whether the operation just now was successful, press Win+R, enter cmd, and enter g++ --version in the console

Even if such an interface appears, it is successful

aec423da35a446809b52894af8edd034.png

 In this way, our g++ is installed!

The third step is to install the VSCode plug-in

Now we open the VSCode we just installed and click this button

bb3c2877a0094fc1bb0fdce84c6e3785.png

 Search C/C++ to install the first plugin (I have configured it before so it is already installed)

0c9277007c9d4b7d92221380317bbf47.png

Then search and install Code Runner

5270e382e3ef4564b21106ce974440d0.png

 If you need the Chinese version, you can search and install the Chinese plug-in (I don’t recommend it personally, if you feel difficult to get started, you can install it if you are a novice)

ba83c482151949dc8cb94ac64bc6efd6.png

There are also many useful plug-ins, such as automatic completion, changing the color of brackets, etc. If you need it, you can search it yourself. I won’t digress here.

The fourth step is to configure the debugging function

This is the most important part of the configuration process! The previous content is also easy to find on various platforms, and then I was stuck at this step for several hours.

First of all, you can create a folder in a location you want, and you can name it at will (note that Chinese is not allowed!), and the future C/C++ code files must be placed in this folder to be able to debug normally.

f6444768c06042ed81bba1835432aabc.png

Here I built it on the desktop.

Then enter VSCode, click Open Folder or click File -> Open Folder in the upper left corner, then open the folder you just created, and choose to trust the parent folder

Click this icon to create a new folder and name it .vscode (note that it must be this name!)

190273d802ed4802881c8bfe360e0c8c.png

a39317efdce740bf8466be00d209b0c2.png

After the creation is complete, click this icon to create four new files. The file names are

//c_cpp_properties.json
//launch.json
//settings.json
//tasks.json

f2416b518f2a451e804286163bede1e1.png

Next copy and paste the contents of these four files 

The first is c_cpp_properties.json

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

Note that the compilerPath item needs to change the path to the installation path of g++ just now: find the installation folder just now->MinGW->bin->g++,exe, then copy or manually type the path of g++.exe, the format should be the same as Same as above code snippet

then launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch", 
      "type": "cppdbg", 
      "request": "launch", 
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 
      "args": [], 
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true, 
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
      "preLaunchTask": "g++",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

Note that the path of miDebuggerPath should also be changed to the installation path of g++ just now: find the installation folder just now->MinGW->bin->gdb,exe, then copy or manually type the path of gdb.exe, the format should be Same as above code snippet

 Next is settings.json

{
  "files.associations": {
    "*.py": "python",
    "iostream": "cpp",
    "*.tcc": "cpp",
    "string": "cpp",
    "unordered_map": "cpp",
    "vector": "cpp",
    "ostream": "cpp",
    "new": "cpp",
    "typeinfo": "cpp",
    "deque": "cpp",
    "initializer_list": "cpp",
    "iosfwd": "cpp",
    "fstream": "cpp",
    "sstream": "cpp",
    "map": "c",
    "stdio.h": "c",
    "algorithm": "cpp",
    "atomic": "cpp",
    "bit": "cpp",
    "cctype": "cpp",
    "clocale": "cpp",
    "cmath": "cpp",
    "compare": "cpp",
    "concepts": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cstring": "cpp",
    "ctime": "cpp",
    "cwchar": "cpp",
    "exception": "cpp",
    "ios": "cpp",
    "istream": "cpp",
    "iterator": "cpp",
    "limits": "cpp",
    "memory": "cpp",
    "random": "cpp",
    "set": "cpp",
    "stack": "cpp",
    "stdexcept": "cpp",
    "streambuf": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "utility": "cpp",
    "xfacet": "cpp",
    "xiosbase": "cpp",
    "xlocale": "cpp",
    "xlocinfo": "cpp",
    "xlocnum": "cpp",
    "xmemory": "cpp",
    "xstddef": "cpp",
    "xstring": "cpp",
    "xtr1common": "cpp",
    "xtree": "cpp",
    "xutility": "cpp",
    "stdlib.h": "c",
    "string.h": "c"
  },
  "editor.suggest.snippetsPreventQuickSuggestions": false,
  "aiXcoder.showTrayIcon": true
}

 Finally tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "g++",
      "command": "g++",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${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
        }
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

 Save these four files and the configuration is complete!

Emphasis again: future C/C++ code files must be placed in this Code folder, or in a folder with a .vscode folder. If you debug code files placed in other locations, an error will be reported!

You can create multiple folders in the Code file to store codes in categories like me.

751f2829cebc4432bdb60d5a04c707b0.png

If you have completed the above process, now you can create a new .c or .cpp file and write code to test the VSCode you just configured! (Note that the file name cannot be in Chinese!)

something else

1. Chinese display garbled characters

This is a very common problem, so when I help others configure it, I usually help out.

First click the gear button in the lower left corner to open Settings

862ff2498ea74f98860f09e1779c9365.png

Enter ecocoding in the search box, and then change Encoding to GBK as shown in the figure (it should be UTF-8)

7d3626a49b104c21a57886018fceee52.png

After setting, editing files with Chinese characters will not display garbled characters!

2. Run in terminal

If you don’t like a small black box popping up every time, you can choose to run it in the terminal, the effect is as shown in the figure

407b8a02a01741aa9fff3c26a33f9c51.png

This setting is also very simple. Open the settings, search for run in terminal, find this option and tick it.

10d4ee6d8d4d44318a67b099e35dcbd5.png

 After that, you can click the button in the upper right corner to run/debug in the terminal below, or press the shortcut key Ctrl+Alt+N to run (personally prefer)

Note that you must save it before running the terminal, otherwise it will run the code before saving (you can check the automatic save in File)

681b100adaef416fa42626433da7b8f8.png

 Of course, you can still choose to press F5 to use the small black box to run and debug

But the two processes are in conflict, there is just one more option, and they cannot be used at the same time! For example, when I am running the code in the terminal, if I press F5 to debug the code, an error will occur

3. When debugging, it shows "cannot find g++"

First check whether there is Chinese in the g++ installation path or file name, if there is Chinese, you need to change the Chinese name or replace it with another path to install

If Chinese does not exist, right-click the VSCode icon and select "Properties"

Then select "Compatibility", check "Run this program as an administrator", and then click "Apply" and click "OK" (some computers need to select this option)

079b422a9a474c6a9998c8c44dfd3b44.png

 

 

Guess you like

Origin blog.csdn.net/m0_62721576/article/details/127207028
Recommended