'How to save c++ code file using visual code?
I downloaded the C++ extension of Visual Studio. How do I save it and how do I open it to see my code? Usually, with HTML, I save as .html and when I open the file, I can see it on google chrome see image
Solution 1:[1]
C++ is very very different from HTML. HTML can, as you know, easily be opened in a browser and viewed.
C++ is a programming language and must be compiled and run. There are many ways to do this and largely depends on the operating system you are using and software you have installed.
Since you are using VSCode you will likely want to use the terminal to compile and run it. Try looking at something like g++. You can find lots of tutorials online just by googling "compiling c++".
You may find it easier when just starting out with C++ to try it out a bit using a site such as repl.it which will let you write simple programs fairly easily.
If you provide a bit more info such as which operating system you are on I can help provide a specific example.
Solution 2:[2]
Will split my answer into three main points:
- Small introduction.
- Installation.
- Additional resources.
Small Introduction
C++ works differently than HTML.
The main difference (there are more!) is that C++ files need to be compiled before they can be used. And that requires a compiler.
There are various compilers available for C++.
Installation
Assuming that OP is using Windows, assuming that VS Code is installed, there are a few additional requirements:
Install C++ extension for VS Code (this one).
Install the latest version of Mingw-w64 (from here). This one provides one with up-to date builds of GCC, Mingw-w64, and other C++ tools/libraries. Make sure you follow all the steps mentioned in MSYS2 installation guide (for a detailed installation guide go here).
Add Mingw-w64 bin folder to Windows PATH environment variable.
3.1. Access Settings and type in the search bar "Edit environment variables for your account"
3.2. In the User variables, select the Variable Path
and click Edit.
3.3. Add the location of the Bin folder, the default should be C:\msys64\mingw64\bin
and click OK.
Note: In order to check if the installation was successful, open a new CMD window, and run g++ --version
. The output should look like
If one runs gdb --version
the output should be
You are now ready to go.
Additional resources
If you want to learn how to create Hello World using C++ on VS Code, go here.
Additional resources that may be relevant:
There are plenty of C++ online compilers that one might want to use for faster testing, such as W3Schools. These can be quite handy as they allow one to view the result in one's browser.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Derek C. |
Solution 2 | Gonçalo Peres |