Programming with Code::Blocks

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Today, we have ‘n’ number of IDEs that convert the routine task of writing thousands of lines of code into a meaningful process. IDEs are smart, productive tools that increase the efficiency of developers. Moreover, they are a combination of editor, compiler, and debugger; intelligent enough to identify and auto complete syntax and typical keywords. Hence, Code::Blocks is too leveraging with a smart IDE. It is an open source, free programming language especially designed for C, C++, and FORTRAN. The first stable version 8.02 of Code::Blocks was in 2008. It supports a variety of compilers, including Microsoft C++, Borland, Intel C++, and GCC. Although Code::Blocks is available on the Linux and MAC platforms, this article deals with Code::Blocks for the Windows platform.

Installation

As said earlier, Code::Blocks is an open source programming language. Hence, its binaries for Windows, Linux, and Mac platform can be downloaded freely from its official website, www.code::block.org. Identifying the correct package is the first essential task, because there are couple distinct packages available, leveraging dispersed features for both Windows and Linux platforms.

CodeBlock1
Figure 1: Choosing an installation package

Once you have downloaded the correct package, its installation is quite easy on Windows. It installs like any other typical software. Finally, the Code::Blocks development environments startup window looks like Figure 2.

CodeBlock2
Figure 2: Code::Blocks IDE’s First view

Code::Blocks in Action

After you are done with installation and subsequent configuration, it’s time to start coding. You will observe a screen appears right after imitating this software, like in Figure 2, that enables you to create a new project and other functionalities.

To start a new project, click ‘Create New Project’ on the screen. Here, you will encounter with a huge list of predefined project templates, as in Figure 3. Go ahead and select “Console Application;” this will allow you to write a program for the console.

Note: The Code Blocks IDE supports the “IntelliSense” feature as well as leverage with other debugging facilities including breakpoints, call stack, memory dump, thread switching, CPU register, and disassembly.

CodeBlock3
Figure 3: Step 1, selecting a project template

The other application templates in Figure 3 are for developing more advanced types of applications. After selecting Console Application, click the Go button to begin using the Console Application Wizard. Henceforth, the wizard will ask to choose the programming language between C and C++ for coding like as mentioned in Figure 4.

CodeBlock4
Figure 4: Step 2, selecting a coding language

The wizard will also prompt you to specify the project name along with its directory location to store and select the default compiler as GNU which the IDE detects automatically as in Figures 5 and 6, respectively.

CodeBlock5       CodeBlock6
Figure 5: Step 3, filling in project settings       Figure 6: Step 4, selecting a compiler

Because you have chosen C++ as the programming language, there is a file called main.cpp with default “Hello Word!” code automatically generated in the solution as shown in Figure 7. At this point, you can debug and compile this code by pressing the F9 key. After compilation, if there is any error in the arbitrary code, the IDE will reflect a visual indication like other IDEs.

CodeBlock7
Figure 7: main.cpp file default code

Because the aforesaid is generated automatically, the following code sample is provided to show a more realistic experience in C++ coding with Code::Blocks. In This code, you calculate the Fahrenheit value from Centigrade.

#include <iostream>

using namespace std;

int main()
{
   float c = 50;
   float f = (c *  9 / 5) + 30;

   cout << "Calculated value of F=" << f <<;
   return 0;
}

Listing 1: Centigrade to Fahrenheit conversion code

After successfully compiling and debugging the program, it produces the calculated Fahrenheit value along with other variables, including execution time in the console window, as follows:

Calculated value of F=120

 

Process returned 0 (0x0)   execution time : 0.063 s

Press any key to continue.

Output 1: Calculated value of Fahrenheit

Essential Configuration for Code::Blocks

As claimed in the earlier paragraph, Code::Block supports numerous compilers. I have choose the GNU GCC compiler in this article, so the necessary configuration settings need to be set that pertain to this compiler. This optional setting aids while the program faces some unexpected errors occurring during the execution related to the Linker, compiler, build options, and directories setting. You can fix such glitches through a dialog box that can be access through Setting | Compiler menu, as follows:

CodeBlock8
Figure 8: Compiler Settings

Moreover, if you have a project with additional existing files, go to the Project menu and select “Add files.” This will bring in the files associated with your program. By clicking Add files to your project, you will bring up a window so you can browse to where your files that you wish to add are. Select any additional file you want to add and press Open. The file will then be added to your project.

You also can use build options to ensure that Debug is running. You can use the Project pull-down menu and click Build Options. Here, make sure that the Produce debugging symbols [-g] is checked, as shown in Figure 9.

CodeBlock9
Figure 9: Debug Settings

Conclusions

This article provided an alternative approach to coding C/C++ programming under a different arena by introducing an open source IDE, Code::Blocks, which supports a wide range of renowned compilers. Although the contents in this article are Windows-centric, things are pretty similar on the inux and Mac platforms, too. I briefly touched some of the essential features of Code::Blocks that might be conducive for you and other developers in terms of improving the efficiency while coding. Moreover, Code::Blocks offers a variety of plug-in and other substantial features to support FORTRAN and assembly language.

References

About the Author

Ajay Yadav is an Author, Cyber Security Specialist, SME, Software Engineer, and System Programmer with more than eight years of work experience. He earned a Master and Bachelor Degree in Computer Science, along with numerous premier professional certifications. For several years, he has been researching Reverse Engineering, Secure Source Coding, Advance Software Debugging, Vulnerability Assessment, System Programming, and Exploit Development. He is a regular contributor to programming journals and assists the developer community with blogs, research articles, tutorials, training material, and books on sophisticated technology. His spare time activity includes tourism, movies, and meditation. He can be reached at ajay007[at]gmail[dot]com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read