How To Compile and Execute C++ Code

Compiling and executing C++ code from Code::Blocks is extraordinarily easy; it’s just a matter of clicking an icon and seeing the result. Here’s how it’s done.

If you have created your first C++ program and now understand the basics behind the structure of one, it’s time to get things moving with the next step: compile and execute, or run if you prefer, the program and see how it looks.

Open Code::Blocks, if you haven’t already, and load up the previously saved Hello World code you created. Ensure that there are no visible errors, such as missing semicolons at the end of the std::cout line.

If your code is looking similar to the one in our screenshot, then look to the menu bar along the top of the screen. Under the Fortran entry in the topmost menu you can see a group of icons: a yellow cog, green play button and a cog/play button together. These are Build, Run, Build and Run functions.

Start by clicking on the Build icon, the yellow cog. At this point, your code has now been run through the Code::Blocks compiler and checked for any errors. You can see the results of the Build by looking to the bottom window pane. Any messages regarding the quality of the code are displayed here.

Now click on the Run icon, the green play button. A command line box appears on your screen displaying the words: Hello, world!, followed by the time it’s taken to execute the code, and asking you press a key to continue. Well done, you just compiled and executed your first C++ program.

Pressing any key in the command line box closes it, returning you to Code::Blocks. Let’s alter the code slightly. Under the #include line, enter:

using namespace std;

Then, delete the std:: part of the Cout line; like so:

cout << “Hello, world\n”;

In order to apply the new changes to the code, you need to re-compile, build, and run it again. This time, however, you can simply click the Build/Run icon, the combined yellow cog and green play button.

Just as we mentioned in the previous guides, you don’t need to have std::cout if you already declare using namespace std; at the beginning of the code. We could have easily clicked the Build/Run icon to begin with but it’s worth going through the available options. You can also see that by building and running, the file has been saved.

Create a deliberate error in the code. Remove the semicolon from the cout line, so it reads:

cout << “Hello, world!\n”

Now click the Build and Run icon again to apply the changes to the code. This time Code::Blocks refuses to execute the code, due to the error you put in. In the Log pane at the bottom of the screen you are informed of the error, in this case: Expected ‘;’ before ‘}’ token, indicating the missing semicolon.

Replace the semicolon and under the cout line, enter a new line to your code:

cout << “And greetings from C++!\n”;

The \n simply adds a new line under the last line of outputted text. Build and Run the code, to display your handiwork.

Russ Ware

Russ has been testing, reviewing and writing guides for tech since the heady days of Windows 95 and the Sega Saturn. A self-confessed (and proud) geek about all things tech, if it has LED's, a screen, beeps or has source code, Russ will want to master it (and very likely take it apart to see how it works...)

Related Articles

Back to top button