skip to Main Content

I recently tried running some code on VS Code but, as I’m using C++11 Standard code, I have to manually change the execution code in terminal to g++ -std=c++11 to actually run the code without errors.

I’m using Code Runner v0.11.8 by Jun Han

How can I set this option as the default?

2

Answers


  1. On this page there is a Configuration section in which you have example how to specify compilation for c language. Write your own for C++ with flags you need and I think that it will be enough.

    Login or Signup to reply.
  2. Follow below steps:

    • Open VsCode settings using CTRL+, or from left bottom side
    • click edit settings.json
    • add the following code snippet in the end:
       {
         "code-runner.executorMap": {
             "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
          }
       }
      

    & it’s done.

    NOTE: Make sure VsCode terminal profile is Command prompt.

    My setup:

    • I use this execution command, it also remove .exe file after execution of the program:
        "code-runner.executorMap": {
      
      // -lm flag for linking c file with libm (will enable use of math.h in VsCode)
          "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -lm && $dir$fileNameWithoutExt.exe && del $dir$fileNameWithoutExt.exe ",
          "cpp":"cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt.exe && $fileNameWithoutExt.exe && del $dir$fileNameWithoutExt.exe",
      
      },
      

    You can use any version like -std=c++14 or -std=c++17.

    Hope it helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search