Chapter 2. App Development with Code::Blocks

In this chapter, we'll learn C++ app development with Code::Blocks. We'll begin with a simple Hello World app. Subsequently concept of project and workspace will be introduced.

Creating your first app with Code::Blocks

Let's write a simple Hello World app, which essentially prints out "Hello World" to console. Launch Code::Blocks to begin and as shown in the following screenshot click on the new button in main toolbar and then click on the File menu option. The following screenshot represents the same:

Creating your first app with Code::Blocks

Click on the C/C++ source option in the next window and then on the Go button. A wizard will be presented. Click on the Next button on the first page of the wizard. Choose the C++ option and click on the Next button. Choose file path and name in the next window and click on the Finish button to complete wizard.

Then type the following code in the editor:

#include <iostream>

int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Code::Blocks will automatically add an empty line at the end of the file if there isn't any, this is a Code::Blocks feature. GCC expects an empty line at the end of source code, and it will throw warning if an empty line is missing. Thus you may notice an empty line is being added automatically by Code::Blocks.

After the code is typed in the editor window Code::Blocks will look similar to the following screenshot.

Creating your first app with Code::Blocks

Now click on the save button in main toolbar to save this file (navigate to File | Save from the dropdown menu bar). Alternatively Ctrl + S key combination can be used to save a file. We can see that Code::Blocks has applied syntax highlighting to the code and it has made the code more readable.

Now click on the build button in the Compiler toolbar or hit Ctrl + F9 key combination to compile it. If everything goes well Code::Blocks will look similar to the previous screenshot. Now click on the run button in Compiler toolbar. Code::Blocks will now run the program. As seen in the following screenshot our first program has run successfully:

Creating your first app with Code::Blocks

The previous screenshot shows that the program execution has been completed and it is waiting for user input to close the window. This is a Code::Blocks feature which stops after the execution is completed in order to allow the users to study program output.

Our first assignment is successful. However, this approach has several drawbacks.

  • Code::Blocks applies global compiler/linker flags during compilation of individual files
  • Code::Blocks behaves purely as a text editor (imagine Notepad) and most features can't be used to compile individual files.

Also management of large projects comprising of individual files is cumbersome. So the concept of Project has evolved. In the next section we'll learn more about projects in Code::Blocks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87