Create a C++ program. Run in Visual Studio Code. 
Tags: cpp, beginners, programming, computerscience
Published: 2020-12-16 23:38:12

Do you think that C++ is too old school to learn? Think again! It is still a highly sought after skill. Learning C++ will better prepare you for learning C# and give you a broader skill set!

Here are the key concepts I am going to review in this blog:
1. Create a hello world program in C++
2. Installing Microsoft Visual Studio Code 
3. Adding C/C++ language extension
4. Install the g++ compiler to compile the source code and GDB
5. Compile and run the program!

---

# Create a hello world program

Create a folder on your desktop that will hold your C++ files and call it whatever you like. Open the notepad and type the following code. 

You could also create your .cpp file in Visual Studio Code. 


```
#include <iostream>
int main() 
{
  std::cout << "Hello World!\n";
  std::cout << "Welcome to C++ in VSCode!\n";
}
```

`#include <iostream>`:
"Pre-processor directive." This code will instruct the compiler to locate the file that contains code for the library known as iostream. 

`int main() {}`:
Every C++ program must have a function called main(). It will house all of the instructions for our program. The main() function is where we will start writing our code. 

`std::cout`:
The 'character output stream"; is pronounced "see-out."
`<<` is an operator

In C++, you need double quotes around the text. Just like in Javascript, the ";" is the punctuation that will tell the computer that you are at the end of a statement. When you are typing text for your output, you need to remember to type `/n` inside the double quotations to move to the next line. 

**Save your word doc as a .cpp file in your new folder.**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e8lc0ozx16hiufbnxp48.jpg)

---

# Installing Microsoft Visual Studio Code

The instructions for [installing Visual Studio Code] (https://code.visualstudio.com/) is very similar on Mac and PC. 

I'm on Windows, So I can follow the installer instructions and keep all of the default options. 

For Mac users, drag the Visual Studio Code app over to your application folder. 

---

# Adding C/C++ language extension
Run VSCode. Under the Welcome tab, click the open folder button. Then select your C++ folder on your desktop.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b0wlzmq9r9qboqyv95n3.jpg) 

You should see your "helloWorld.cpp" file. When you select it, you should see a popup for recommended extensions that can run it. Or, you can click on your extensions tab on the left and search for one. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c7jbb5hmsgj9r9cahd7e.jpg)

---

# Install the g++ compiler to compile the source code and GDB
*Mac User? You have it easy (surprise surprise); skip this step.*

Windows User? This guide will go through this step in more detail:
https://code.visualstudio.com/docs/cpp/config-wsl.

I can summarize the steps for you. You will have to install the g++ compiler to compile the source code and GDB for debugging. These tools do not come by default on Ubuntu, so follow these steps in your bash shell for WSL. 

1. Run `apt-get update` to update the Ubuntu package lists. (An out-of-date distro can sometimes interfere with attempts to install new packages).

```
sudo apt-get update
```

2. Now From the command prompt, install the GNU compiler tools and the GDB debugger by typing:

```
sudo apt-get install build-essential gdb
```

There you go, now you should be caught up to the glorious Mac users.
 
---

# Compile and run the program!

Now we are going to compile our .cpp file. In your bash terminal.

The command to compile a file is `g++`. 

If you would like to give the executable file a specific name, you can do this by typing `-o` and the title after your .cpp file.  

```
g++ helloWorld.cpp -o HelloWorld
```

Now that our .cpp program has been translated into machine language. Run it. 

```
./helloWorld
```

Great work. Your first C++ program. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hoboc7rgukoowxk4vfyy.jpg)

---

**Stay Focused || Love your code**