“Hello World” in C++

Dalya Gartzman
codeburst
Published in
4 min readMay 10, 2020

--

After several years of writing code in Matlab and Python, I honestly didn’t think writing a “Hello World” program in C++ would take me more than two hours to complete. But life is full of surprises, right?

My goal in this post is to help the next person have a smoother transition to C++ by troubleshooting some of the surprises.

Note: my OS is Ubuntu 18.04, but from the research I conducted for this post, I suspect that everything here is relevant for other OSs as well.

The steps we are going to cover in this post are:

  1. Install Visual Studio Code (VS-Code).
  2. Install mandatory extensions to run C++ code in VS-Code: C/C++ and Code Runner.
  3. Write a “Hello World” program in C++.
  4. Make the program work properly (yes, we need a separate item for this!)

1. Install Visual Studio Code (VS-Code)

This is luckily an easy one: go here, download the installer compatible with your OS, and install.

Note: After writing this, I received several recommendations to use CLion instead of VS-Code. If I get around to following up on this advice, I will update here :)

2. Install Mandatory Extensions to Run C++

Although the internet says that VS-Code is a perfect IDE to write C++ code, we still need to install 2 extensions to make this actually work (yeah! I know! they are not built-in!)

What did I ask for? An IDE for C++ that can compile and run C++?

2.1 Install C/C++

Open VS-Code, go to the extensions manager, search for C/C++ extension, and press “Install.”

If this stage did not go smoothly, read on.

2.2 Install Code Runner

One might speculate that installing a C++ extension is enough to run C++ code. One might be wrong to forget that life is surprising. One also needs to install Code Runner.

After installing Code Runner, you can run your code using Alt+Ctrl+n

2.3 Troubleshooting C/C++ installation

If the C/C++ installation did not go smoothly, know you are not alone!

For example, you might get the error Error: end of central directory record signature not found which might be followed by this “helpful” note:

If you work in an offline environment or repeatedly see this error, try downloading a version of the extension with all the dependencies pre-included from https://github.com/Microsoft/vscode-cpptools/releases, then use the “Install from VSIX” command in VS Code to install it.
Ho! VSIX! not MEESEEKS!

mmm….

Let’s decipher this into clear, actionable items:

  1. Go here.
  2. Scroll down. Below Known Issues, you will see a tiny mysterious tab called Assets. Open it and download the file compatible with your OS.
  3. What does “Install from VSIX” mean??? Well, you are right to ask! 🧐
    Open VS-Code, open the extensions browser, press the ... on the top right, select “Install from VSIX”, choose the file you just downloaded, and install.
  4. Restart VS-Code.
“Install from VSIX” with “just” 7 simple steps 🙄
Every time I see such a cryptic “help” message, it brings back the dreadful “it’s easy to see…” in math classes.

3. Write a “Hello World” Program in C++

Now we are finally ready to write our “Hello World” program in C++!

I just started this course, which is free for the first month on Linkedin Learning, and so far, I like it a lot. It taught me how to write this “Hello World” C++ program:

4. Make the Program Work Properly

TLDR: You need to save the program every time you make changes!

After years of developing in PyCharm, which autosaves your every move, the requirement to actively save my program came as a complete surprise to me.

And how come I discovered this requirement so early into my C++ programming? Well, this one is on me. I am terrible at copy-pasting. Isn’t everyone? 😳
I followed the instructions in the course: opened a file called hello_world.cpp, copied into it the content of the example file above, completely missing the line using namespace std;.

So then, when I ran the code, I got an error:

hello_world.cpp:6:5: error: ‘cout’ was not declared in this scope

After realizing what my problem was, I added the missing line, re-ran the code, and still! Got the same error again!

Only when it occurred to me to save the file, did the program finally run as expected 😅

woohoo!
To prevent this disaster from happening again, you can enable autosave :)
Finally a Happy Ending :)

--

--