0

So I just got into using VS Code. I am currently working with C++ and I am using Mingw as my compiler. So far I have been using the terminal at the bottom of VS Code to compile and run like this:

g++ program.cpp then doing ./program.exe

The process of using the terminal at the bottom is time consuming especially when I need to compile and run code frequently. This is also annoying when creating classes when you have to compile multiple files into .o extensions etc.

Can anyone help with this process? Am I doing something wrong, should there be an easier way? Maybe a makefile?

Thanks!

rioV8
  • 24,506
  • 3
  • 32
  • 49
tyler.904
  • 19
  • 6
  • Have you read [Using GCC with MinGW](https://code.visualstudio.com/docs/cpp/config-mingw)? – Some programmer dude Aug 21 '21 at 16:06
  • [You bind your command to a hotkey.](https://stackoverflow.com/q/59844962/2752075) (the question says .bat files, but it works for custom commands too). For multiple files yes, you'd combine this with Makefiles, or any other build system like CMake. – HolyBlackCat Aug 21 '21 at 16:07
  • 3
    I also recommend you learn how to use build systems like `make` or Ninja. Or perhaps a meta-build system like CMake or Meson, which can be used to generate projects and build files. – Some programmer dude Aug 21 '21 at 16:07
  • 1
    https://code.visualstudio.com/docs/languages/cpp you have all you need here – bolov Aug 21 '21 at 16:32
  • Try using the coderunner extension for c++, it can execute programs with the run button – umar Aug 21 '21 at 16:53
  • Cmake with the visual studio cmake plugin is probably the simplest way to build c++ in vs code – Alan Birtles Aug 21 '21 at 19:32
  • @Someprogrammerdude I am attempting to follow that guide (which I had up a while ago!). My problem is, when I use the linked MingW Installer (https://www.msys2.org/), it installs everything except the `bin` folder is empty! I have tried it multiple times and every time I install, the `bin` folders have no contents. If I am right, that is where all of the `.exe` files should go.... Not sure how to proceed – tyler.904 Aug 21 '21 at 22:51
  • Learn to use [GNU make](https://www.gnu.org/software/make/) and read the documentation of [GCC](https://gcc.gnu.org/). You want to run `g++ -Wall -Wextra -g program.cpp -o program.exe` at least – Basile Starynkevitch Jun 07 '22 at 13:55

1 Answers1

0

If you want to compile and run C++ code in visual studio code(Vs-code) in windows. This include following steps.

  1. Download Visual studio code.
  2. Go on Add extension Type C++ and install "C/C++" by Microsoft.
  3. Go to Visual Code studio docs for "C++" OR https://code.visualstudio.com/docs/languages/cpp
  4. Install MinGW-x64 vis MSYS2 website and run this on shell "pacman -S --needed base-devel mingw-w64-x86_64-toolchain"
  5. Then go to windows setting and look for Edit environment variables for your account. Then in advance settings >> Environment Variable.
  6. In "system variable" choose path and edit it and add a new path.
  7. You can find a new path in your directory where you have installed the MinGW-x64. you might find it in C:\msys64\mingw64\bin. or where ever you have installed it.
  8. When you have added the new path then go to any shell/cmd and Type g++ --version
  9. if you get the version then you have succeded.
  10. If you find something like command not recognized then please check where you have done wrong or skipped any step.
  11. Otherwise startover.

thanks--

shashank
  • 61
  • 1
  • 6