Need help setting up visual studio code

Hello everyone, I’m trying to start using VSCode to code. I have a couple concerns with the app:

  1. When I type a for loop like so:
    for(int i=0;i<n;++i) {
    When I try to press enter after the first bracket, it doesn’t automatically indent for me and make a new line like this ("|" being where my typing thing is)
    for(int i=0;i<n;++i) {
    |
    }
    and instead looks like this
    for(int i=0;i<n;++i) {
    |}

This is a very unpleasant issue I didn’t have to deal with in online IDEs.

  1. When I make a 2D vector like such:
    vector<vector> v;
    I get a compile error saying I need to put a space in between the last 2 ">"s like this:
    vector<vector > v;
    While this is a minor inconvenience, I don’t really like it and would prefer knowing how to fix it.

  2. Auto formatter
    When right-clicking a place in my code, there is an option to automatically format my code (a beautifier).
    This is what my code originally looked like:
    image
    This is what my code looked like after formatting:
    image
    While I want it to look like
    image
    Is there any similar preset I can somehow use to do this? And how do I set the preset?

1 Like
  1. I don’t know about the first issue, since I got the first output.
  2. You should select “C/C++: Edit Configurations (UI)” from the command palette and set your C++ standard to C++11 or more recent.

  1. You’ll need a .clang-format file in your project directory: see some of the links from Adding Solutions. Alternatively, you can search “formatting” in settings and set “C_Cpp: Clang_format_fallback Style.”

To not break before braces, you can start with the visual studio style and remove “BreakBeforeBraces: Allman.”

1 Like

Hey Benq,
The solution to my third problem worked. However, after changing the C standard and C++ Standard to c17 and c++17, it still gives me the error for the vector initialization
image

 error: a space is required between consecutive right angle brackets (use '> >')
  vector<vector<int>> v;
                   ^~
                   > >

I reset my settings and modified some that I think were very important, and most my problems were solved. I still need the solution to the 2d vector problem though.

The screenshot you sent is a little different from the one in my response; did you try going to “C/C++: Edit Configurations (UI)” and editing the C++ standard there?

Yes, I did. I did cmd+shift+P and searched C/C++ Edit Configurations (UI) and it took me to this place. Here is the full screenshot:

If it helps, here is my settings.json

{
    "window.zoomLevel": 1,
    "C_Cpp.default.cppStandard": "c++17",
    "C_Cpp.default.cStandard": "c17",
    "workbench.colorTheme": "One Dark Modern",
    "[cpp]": {
        "editor.defaultFormatter": "xaver.clang-format"
    },
    "C_Cpp.clang_format_fallbackStyle": "Google",
    "C_Cpp.clang_format_style": "",
    "debug.terminal.clearBeforeReusing": true,
    "code-runner.clearPreviousOutput": true,
    "files.autoSave": "afterDelay",
    "code-runner.runInTerminal": true,
    "clang-format.fallbackStyle": "Google",
    "editor.defaultFormatter": "ms-vscode.cpptools",
    "clang-format.executable": "",
    "editor.indentSize": "tabSize",
    "editor.detectIndentation": false,
    "editor.bracketPairColorization.enabled": false,
    "C_Cpp.enhancedColorization": "disabled"
}

Does changing the C++ standard change the intellisense messages you get at all? For this code:

#include <vector>

using namespace std;

int main() {
	vector<vector<int>> v;
	pair<int, int> p;
	auto [l, r] = p;
}

I get the following errors when I select c++03, but they disappear when I switch to c++17.

Alternatively, even if I have my standard set to c++03, adding -std=c++17 to the compiler arguments makes the errors go away:

(Also, maybe we’re using different compilers? Idk if that matters.)

I tried changing the C++ standard to c++03 and nothing changed.
Also, I was using GCC and changed to clang++, but it didn’t change anything either.
I tried putting -std=c++17 in the compiler arguments and nothing changed either.

Do you see anything happen at all when you change the compiler arguments or C++ standard? For example, if you switch between c++98 and c++03, you should see the list of problems disappear briefly and then reappear. Same for the compiler arguments.

(Note: for the compiler arguments, you need to click outside of the text box after typing them in for them to take effect.)

Yes, the errors disappear and then reappear.

Don’t have any other ideas for stuff to try, but if you resolve this please let us know how you did. :slight_smile:

When I run your code with c++17 and c++03, here are all the errors (all the same):

test.cpp:6:19: error: a space is required between consecutive right angle brackets (use '> >')
        vector<vector<int>> v;
                         ^~
                         > >
test.cpp:8:2: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
        auto [l, r] = p;
        ^
test.cpp:8:7: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
        auto [l, r] = p;
             ^~~~~~
test.cpp:8:8: error: binding reference of type 'std::__1::tuple_element<0, std::__1::pair<int, int> >::type' (aka 'int') to value of type 'const typename tuple_element<0UL, pair<int, int> >::type' (aka 'const int') drops 'const' qualifier
        auto [l, r] = p;
              ^
test.cpp:8:8: note: in implicit initialization of binding declaration 'l'
2 warnings and 2 errors generated.

Also I have another problem. When I try to auto-format, it doesn’t work, saying “The ‘clang-format’ command is not available. Please check your clang-format.executable user setting and ensure it is installed.”
I’m not sure how to fix this.

By “run my code” do you mean you are executing g++ -std=c++03 test.cpp from the terminal? That produces the errors you showed above, but g++ -std=c++17 test.cpp shouldn’t produce any.

I thought formatting was working for you, what changed? Anyways, usage · Issue #48 · xaverh/vscode-clang-format · GitHub looks relevant.

Could you post a screenshot of the compile errors you are getting? Is this related to the problems panel as I posted here, or is this something else?

When I say run, I am clicking the play button at the top right corner. When I tried using your command g++ -std=c++17 test.cpp it worked. But I don’t really want to type this command every time I want to run my code, is there another way to run my code or make this my default running command?

For your second post:
Problems panel:


I get these problems with c++03, and I don’t get the same errors with c++17.
Terminal:
image
These compile errors are the same with both versions.

Ok, you’ll need to edit tasks.json as described here: Configure VS Code for Clang/LLVM on macOS

I changed my tasks.json to this

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "C/C++: clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": "build",
        "detail": "Task generated by Debugger."
      }
    ]
  }

It still doesn’t work.

After a month of searching, I found the answer. All I had to do was paste this segment into my settings.json.

    "code-runner.executorMap":{
        "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    },