DEV Community

Cover image for Create a C++ program. Run in Visual Studio Code.
JerryMcDonald
JerryMcDonald

Posted on • Updated on

Create a C++ program. Run in Visual Studio Code.

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";
}
Enter fullscreen mode Exit fullscreen mode

#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


Installing Microsoft Visual Studio Code

The instructions for installing Visual Studio Code 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

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


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
Enter fullscreen mode Exit fullscreen mode
  1. Now From the command prompt, install the GNU compiler tools and the GDB debugger by typing:
sudo apt-get install build-essential gdb
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

./helloWorld
Enter fullscreen mode Exit fullscreen mode

Great work. Your first C++ program.

Alt Text


Stay Focused || Love your code

Top comments (2)

Collapse
 
dynamicsquid profile image
DynamicSquid

Why didn't you create a file using VSCode instead of Notepad?

Collapse
 
jerrymcdonald profile image
JerryMcDonald • Edited

Thanks for the input. I wanted to show how easy it is to get started. A great addition to this blog would be to show how to compile your .gpp file from Windows Command Prompt or a Mac Terminal. I will go the route you suggest in future blogs of the series.