Exercise 1. Hello world!

Welcome to the course!

We'll be using Wandbox to deliver the code for these exercises. Wandbox is an online C++ compiler; that is, it's a Web interface to a plain old C++ compiler running somewhere on the back-end. Wandbox supports both GCC and Clang.

Wandbox lets you type in some C++ code and hit "Run" to compile and run it. After you've hit "Run", Wandbox also gives you the option to "Share" a permalink to your code. The shareable permalink is a snapshot of your code as it appeared when you hit "Share". It is not a shared-editing session like a Google Doc. When you or someone else visits the permalink, they'll get a copy of the code; modifying the copy won't affect the original. So this makes it a great way to distribute the code for these exercises!

(In case Wandbox goes down during the class, I've got backup copies of all the code on this website. The raw .cc files are linked right next to the links to Wandbox. If GitHub goes down during the class, we'll have trouble.)

The upside is that you can edit the code at these permalinks any way you want; your solutions will be visible only to you, not to everyone. The downside is that I won't be able to see your solutions either, unless I come look over your shoulder. So I have no very good way of finding out when everyone's done with the exercise, nor if anyone's stumped.

Before each exercise set, I'll set my phone's timer for 10 minutes. If it looks like I'm forgetting to do this, please remind me!

If you're completely stumped after a few minutes, feel free to raise your hand and I'll come over to you. (Or one of your fellow classmates might see your hand first and be able to help!)

If you're done before the timer is up, feel free to play around in Wandbox, or do your own stuff. Each page of exercises ends with some random links to blog posts that I think are worth passing along to you.

Exercise 1a. Hello wandbox

Open this link (backup). You should see a "hello world" program.

1. Find the "Run" button and click it to run the code.

2. Edit the code so that it prints "Hello wandbox" instead of "Hello world". Run it again, using the compiler of your choice.

3. Find the "Share" button and click it to get a permalink to your "Hello wandbox" code.

4. (Optional) Email your permalink to Arthur O'Dwyer <arthur.j.odwyer@gmail.com>. I won't be checking my email as part of the class, but now we'll have each other's email addresses if you want to ask any followup questions during or after this week's conference.

Exercise 1b. Multiple files on Wandbox

Open this wandbox (backup, backup).

1. Try to run the code. It should give you a compiler warning and a linker error.

2. Fix the compiler warning by adding the missing ", %d" to the format string.

3. Fix the linker error by adding a new file to the wandbox. Click the heavy "+" icon next to the "beta.h" tab. Click "noname-3" to switch to that tab. Click it again to rename the new file; enter its new name as "beta.cc". Fill in "beta.cc" yourself with something that will make the program work.

Look at the left sidebar, under "Compiler Options". Notice that there are already three custom compiler options there: -W -Wall -pedantic. Add beta.cc to the list of compiler options so that Wandbox knows it's supposed to compile it. Click "Run" to compile and run your multiple-file program!

Exercise 1c. Experimental options

Open this wandbox (backup). This program uses the Concepts syntax currently slated for inclusion in C++20. This syntax is not standard C++17, but the GCC compiler can support it under an extension flag.

1. Change the compiler from Clang to GCC, and add the flag -fconcepts to GCC's compiler options. Run the code. (It should work.)

Exercise 1d. Godbolt and Rextester

Open this godbolt (backup). Godbolt, unlike Wandbox, shows you the assembly output of the compiler. Within the past year, Godbolt gained the ability to link and run the programs remotely; but only if they fit in a single source file and don't link against any external libraries (such as Boost).

1. At the top of the right pane, you can change the compiler. Change it to "x86-64 gcc (trunk)". Notice that the assembly output instantly refreshes; there is no "Run" button on Godbolt.

To change the compiler quickly: Click the dropdown and hit backspace, then type "gcc trunk" and hit enter.

2. Change the compiler options to include -O2. Notice that the assembly output refreshes --- and it gets much shorter!

3. Change the compiler to "x64 msvc 19.22". This basically means "Visual Studio 2019." Godbolt supports not just GCC and Clang but also Microsoft Visual Studio and Intel ICC. (It also supports ELLCC and Zapcc which are forks of Clang, and DJGPP which is an old-school fork of GCC.)

4. Click the "Share" dropdown in the upper right corner of the screen to get a shareable permalink to your Visual Studio codegen.

5. Play around with the nine checkboxes underneath the compiler-selection dropdown. They mostly the filter applied to the assembly code: exclude comments, flatten whitespace, show machine code, et cetera. The settings I picked for this exercise seem like the cleanest ones. The most "fun" one is labeled ./a.out and controls whether to actually link and run the program, or just compile it to assembly.

6. Open this rextester (backup). Click the "Run it" button below the source-code entry box. Edit the program to print "hello rextester" instead of "hello world". Click "Run it". To get a shareable permalink, click "Put on a wall"; enter something as the snippet's title (doesn't matter), select "Code wall", and click "OK".

As far as I know, Rextester is the only freely accessible online compiler service that supports both running the code and compiling with Visual Studio. But I think its version of Visual Studio is pretty old.


You're done with the first set of exercises! Sit back and relax, or optionally, browse this collection of computer-related Usenet posts.