VS Code in conjunction with C++ (windows)

Help run (i.e. build (build) and debug) c++ code in VSCode on windows 10. I already have VS Community 2017 installed and the C/C++ extension.
You often have to open single-file programs. That's what I want to use VS Code for. The possibility of debag is not important, but if there is a possibility, then tell me.

#include <iostream>

int main(){
    std::cout<<"Hello World!";
}

What I'm already doing. Using this instruction, I can build a project.
To do this:
1. Install the plugin and restart the program
2. Open VSCode.
3. "File-Open folder" and select the folder where the code is located
4. In the folder with your code, create a file: build.bat, where we insert this code:

@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
set compilerflags=/Od /Zi /EHsc
set linkerflags=/OUT:hello.exe
cl.exe %compilerflags% replay.cpp /link %linkerflags%

5. In the folder open the file with the code and press ctrl+shift+B
6. enter a description of the image here 7. enter a description of the image here 8. enter a description of the image here 9. In the public folder, you receive the folder ".vscode" and in it "tasks.json"enter a description of the image here 10. In place of this insert this:

{
   "version": "0.1.0",
    "windows": {
        "command": "build.bat",
        "isShellCommand": true,
        "showOutput": "always"
    }
}

11. Press ctrl+shift+B again and see that the file has been created hello.exe and others.enter a description of the image here

Is there any way to automate this process? Even in the bat file, you can somehow pass the name of the cpp file, so that the exe would be with the same name? What's wrong with my encoding maybe who knows? And I can't start debugging.

Author: Kto To, 2018-01-09

1 answers

If I understood everything correctly, then I want a simple thing - to be able to open a file with c++ code (a simple hello-word) and be able to compile-run it with one button (I also wanted this and vscode provides such an opportunity to easily do it in Linux, but not in Windows).

I found the following method.

  • Install the extension CodeRunner
  • Now you need to configure the plug-in. Open the settings (Ctrl+,) and look for the section " code-runner. executorMap", next to it, click the pencil and a piece of json with the settings is copied to the right of the window. There we search for our extension (cpp) and see there for gcc. We change it to this line "cpp": "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\VC\\Auxiliary\\Build\\vcvars64.bat\" && cd $dir && cl $fileName /Fe$fileNameWithoutExt.exe && $dir$fileNameWithoutExt.exe"
  • done

In the line at the very beginning, a batch file is started that configures the environment for the compiler. You may need to search for it yourself and replace the path/name (and this will definitely need to be done if the windows is 32-bit, for example)

That's all, now just open the c++ file and from above on the right there is a triangle for compile-run.

You can try to configure debugging, but I didn't get to that (everything works under Linux:)).

 2
Author: KoVadim, 2018-01-10 07:22:08