Home c++ How to configure Visual Studio Code to work with C++?

How to configure Visual Studio Code to work with C++?

Author

Date

Category

How to configure Visual Studio Code to work with C++ after installing the right plugins? Visual Studio does not fit, eats a lot of resources, and the functionality is a bit different.


Answer 1, Authority 100%

Dear UserNameUserName,

About a year ago, I could not adjust the VS Code from the first time.

How rightly indicated in the comments – there is a beautiful Official documentation for setting up . However, I will leave my answer, as it is more detailed in some points. And in other points – less detailed: -)

To configure VSC, it would be good to learn how to compile and run your program from the command line. And then the settings that have been needed for this, add to the right places in the VS Code configuration files.

The difficultness of this process is in the “zoo” of different opportunities. This should not be afraid.

In addition, the setting is slightly different for different systems (Linux, Windows, MacOS), and you did not specify which system you have.

I will cite an example setting for the GCC compiler under Win64. You can repeat my steps, and when it turns out – make self-restarting, for example, to another compiler.

  1. Install MSYS2. I installed it in C: \ PROGRAMS \ MSYS64 . At the same time, MSYS2 himself took care that the path to its directory C: \ Programs \ MSYS64 \ Mingw64 \ MSYS64 \ Mingw64 \ Bin was in Path. You can check this by completing the path

    command in the console

  2. Install the compiler and debugger. I do this using the MSYS2 package by running in the window that opens MSYS2, the following commands:

    pacman -syuu
    

    This command updates MSYS2. It can close the console – this is normal, you need to restart it and enter the same command to complete the update. A good idea will be updated from time to time to always have the latest version of the compiler.

    then:

    pacman -s mingw-w64-x86_64-gcc
     PACMAN -S MINGW-W64-X86_64-GDB
    

Now there is a compiler and debugger on your system. Check it easy: open a new console window, write g ++ --version

If the answer is not the version – you need to look for what went wrong. Check PATH, perhaps edit it manually.

The same check would be good for debugger: GDB --Version

  1. We write Hello WORLD. This will allow us to finally make sure that the compiler works. In any directory, the HELLO.CPP file with the text

    #include & lt; iostream & gt;
     INT MAIN () {
       STD :: COUT & LT; & LT; "Hello World!" & lt; & lt; STD :: ENDL;
       Return 0;
     };
    

later in this folder on the command line compile the G ++ Hello.cpp -o Hello.exe
If the hello.exe file appears, and it starts and drives the line – OK, this step is completed.

  1. And now you can put VSC. Please note that there are several editions, first for 32 and 64 bit systems, and secondly, what will be called “User Installer” and “System Installer”. Select 64 bit System Installer on Download page

  2. in VSC set an extension to work with C++, it is called C / C++ for Visual Studio Code and written Microsoft

  3. Now run VSC in the folder of your project. Let it be the C: \ Projects \ folder. This is done like this: launch console. Go to the console in the folder (CD C: \ Projects \ ). We run the VSC command code. .

It is important that the VSC settings folder are created, which are on the way C: \ Projects \ .vscode . In this folder you need to create 4 files, I will give them in the minimum option. Ways – for my system, where MSYS2 is installed in C: \ Programs \ MSYS64 \ .

The file tasks.json – is responsible for ensuring that working key combination Ctrl + shift + B to build the program:

{
    "Version": "2.0.0",
    "Tasks": [
      {
        "TYPE": "Shell",
        "Label": "g ++ exe build active file.",
        "Command": "C: / Programs / msys64 / mingw64 / bin / g ++ exe.",
        "Args": [
          "-Std = C++ 17"
          "-G",
          "$ {File}",
          "-O",
          "$ {FileDirname} \\ $ {fileBasenameNoExtension} .exe"
        ],
        "Options": {
          "Cwd": "C: \\ Programs \\ msys64 \\ mingw64 \\ bin"
        },
        "PROBLEMMATCHER": [
          "$ GCC"
        ],
        "Group": {
          "Kind": "Build",
          "ISDEFAULT": TRUE
        }
      }
    ]
  }

The file launch.json – is responsible for the debugger:

{
    "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": "C: \\ Programs \\ msys64 \\ mingw64 \\ bin \\ gdb.exe",
        "SetupCommands": [
          {
            "Description": "Enable Pretty-Printing for GDB",
            "text": "-enable-pretty-printing",
            "IgnoreFailures": True
          }
        ],
        "PreLaunchTask": "g ++ exe build active file.",
        "InternalConsoleOptions": "neverOpen"
      }
    ]
  }

The file settings.json – as it is responsible for file associations, perhaps even that he is not particularly needed. But there will be more than:

{
    "Files.associations": {
      "Ostream": "cpp",
      "Iostream": "cpp",
      "Iomanip": "cpp",
      "Chrono": "cpp",
      "Iosfwd": "cpp",
      "Thread": "cpp",
      "Array": "cpp",
      "String_view": "cpp",
      "Initializer_list": "cpp",
      "Utility": "cpp",
      "Valarray": "cpp",
      "Optional": "cpp",
      "Sstream": "cpp"
    }
  }

The file c_cpp_properties.json – is responsible for the location of the include – file:

{
    "Configurations": [
      {
        "Name": "Win32",
        "IncludePath": [
          "$ {WorkspaceFolder} / **",
          "C: / Programs / msys64 / mingw64 / include / **"
        ],
        "Defines": [
          "_DEBUG",
          "UNICODE",
          "_UNICODE"
        ],
        "WindowsSdkVersion": "8.1",
        "CompilerPath": "C: \\ Programs \\ msys64 \\ mingw64 \\ bin \\ g ++ exe.",
        "CStandard": "c11",
        "CppStandard": "C++ 17"
        "IntelliSenseMode": "gcc-x86"
      }
    ],
    "Version": 4
  }

If you create these files, and then re-run in this directory VSC – that everything is supposed to work. That is, a C++ program will be compiled, run in debugging (on F5) and display the values ​​of variables in the debugger window.

Now – the most important thing. Once earned – You need to copy this folder .vscode to the root of each folder with a project in C++. This will allow the VSC start with your settings.

Even if my method does not suit you directly, you now have all the necessary keywords to use them for googling.

Success!

PS. From time to time you come across useful additional options for customization.

Here is a fragment of the tasks.json file that allows you to clear the terminal window before each build – useful so that you no longer see the already fixed errors left over from the previous build attempt (Source ):

"presentation": {
      "clear": true // & lt; - this line
   }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions