View Course Path

How to use Code Blocks – Familiarizing yourself with the IDE

A creators creativity is only limited by his tools. In this post, we will learn about the different tools and hot keys available in Code Blocks that can be used to assist and streamline our programming approach. Moreover, if you know how to use Code Blocks and all its tools, it can make the process of coding a bit less intimidating. And that is something every beginner can use. Please note that this is just a basic familiarization with the IDE.

Code blocks IDE environment familiarization - Intro screen or editor

So this is the first screen that you are going to see when you start Code Blocks. If you see some extra tabs or toolbars, just ignore them, we will get to their usage a couple of steps down this post. The tab marked ‘Start Here’ is where we will be writing all our code. It’s called the Editor. You can also see some shortcuts. Create a project or Open an existing project. In the first post where we learned how to execute our first C program in Code blocks, we had created a new project and proceeded from there. You don’t always have to do that.

We need to understand how to manage our code. In the beginning, it’s going to be very simple. A single file of code, execution, debugging and that will be it. But as you progress through this course and the complexity of your projects increase, you will notice that you will be solving problems using multiple files and maybe even custom libraries. In short, file management is a skill that you should know first and foremost.

See that drop-down menu titled ‘View’ in the top-right region of the window. Select the ‘Manager’ option from it.

Management of files - Code blocks IDE environment familiarization 2
Here’s what your IDE should look like now

The Workspace is self-explanatory. It’s a collection of all the different projects and their files that are currently open in code blocks. Now go back to the same drop-down menu View > Toolbars > Main. The following toolbar should prop up right below the drop-down menus.

Code blocks IDE environment familiarization - Main toolbar

Using the Management window and the Main toolbar we will control our code files.

Creating a New Project in code blocks

A new project is the mother load of any problem set that you are working on. All the code files (known as Sources), ASM files (similar to Sources, but in Assembly level language), header files (like stdio.h) and pretty much everything else is stored in the Project. All projects have an extension of .cbp.

You can also add multiple files to the same folder and create your own libraries ( a handy way to speed up your workflow on large codes). Interestingly, you can also change the compiler for a particular file within the project. Basically, you can control the entire project’s settings from the Management tab.

How can I run some code without creating a new project in Code Blocks?

Suppose you wrote a few lines of code in a class and you wish to perform a quick and simple check on that code. Creating a new project can be a hassle in this case. Just click the new file icon on the Main toolbar and select the highlighted option.

Code blocks IDE environment familiarization - checking code without creating a new project

Select C/C++ source.

Give your file a valid path and start coding. Note that using a file without creating a project first, though simpler to check small snippets of code, won’t allow you access to the debugger tool for that particular file.

How to write code?

We’ve seen how to type in the code in the Editor window in the last post. Let’s add to that to make writing the code a bit easier for you.

If you are someone migrating from a DOS based IDE like Turbo C++, you will need to change a few things that will not work in code blocks. For example, you’ll need to swap out void main() with int main(). We will highlight these differences as we begin coding.

How to add a C/C++ custom help file to code blocks?

Imagine this scenario. There’s a C problem that you’re having trouble with. So you wander into the realm of online help and ask around for help. If you’re lucky enough, some kind bloke might give you a code snippet to try. You bring that code into Code Blocks, build it and it works! But now you have to understand the stranger’s code. There’s an instruction you don’t quite understand. Instead of googling it, you can just check everything about that instruction within Code Blocks!

For this, you’ll need to add a custom help file to Code Blocks. Let’s do that.

Download the CPP CHM file from here. Head on to Settings>Environment>Help Files.

how to add help file in code blocks

Click on add and give it a name. Let’s call it cpp. Locate the file in your downloads folder and add it to the IDE. Don’t forget to check the box that makes it the default help file. You can now just highlight the instruction you want to know the meaning of and press F1.

how to use code blocks - how to add help file in code blocks 2
It’s pretty comprehensive

Tools for good coding practices

A good piece of code always compiles with no errors or warnings. Code blocks, by default, has a low threshold to check for warnings. But you can and should increase this threshold so that your code efficiency and quality increases. Here’s how you can do it.

how to use code blocks - compiler settings

Navigate to Settings>Compiler. And check the two boxes as shown above.

On the same premise, code blocks also offer a certain level of code-refactoring. Code refactoring is a method where you can clean out and structure your code by removing or modifying the code. You can shift around snippets, rename stuff, change different levels etc. You can do all of that without changing the behavior of the code. So the end result is a leaner, well-structured code that performs the same way it did before code refactoring. You won’t need this initially, but you should remember it because we will be using Code Blocks’ (limited) code-refactoring features.

Compiling your code

When you first start a new project, you will notice the window shown below. Make sure you’ve selected the Debug and Release options. These two options will build files that will allow you to debug your code and execute it.

how to use code blocks - debug and release - compiling your code
Ensure that your project has the Debug and Release builds

So once you’ve written your program, you need to compile it. As seen in our previous posts, we will be using the GCC compiler for this. The compiler converts our code into machine code that is understandable by the computer.

From the ‘View’ drop-down menu, navigate to toolbars and select the Compiler toolbar.

how to use code blocks - compiler toolbar

The first icon, the yellow gear, is used to Build the project. This entails compiling the code, checking for logical or syntax errors, building the executable file, debug file and release file. Assuming there is no error in your code, you can now click the Run tool to execute the code. The third icon combines the Build and Run options.

However, if you have errors in your code, it won’t compile. Go to the View drop-down menu again and select the option titled Logs. A new window should now be visible at the bottom of your screen. Within this window, you can see a tab titled Build messages. This tab keeps a log of your code compilation errors and warnings with the exact location of the error.

Debugging Tools

When you execute a code, the final output that you get is the one you choose to be displayed. Suppose you are writing a program to add a = 2, b =3 and then multiply the result with c = 8 and display the final answer. What you will see on-screen is the final answer, i.e 40. What if you want to find the value at step 2 though, what if you wish to find the value of the Sum of 2 and 3? Yes, you can print it too. But this was a simple example, now blow this up into some code where you have hundreds of variables and different functions. It will be very time consuming to print the value of every variable at every step.

The Debug tool can do that for you. Essentially, it gives you an idea of how your program is performing. So that you can check for errors and then correct them.

Generally here’s how it works. You write the code, set breakpoints (the debugger will execute the code only till it reaches the breakpoint, and then select a list of variables that you wish to keep a track off. Then you debug the code. And you can check the values of the variables you had selected to watch.

Keyboard Shortcuts

Imagine Mark Zuckerberg fumbling for the mouse every few seconds in the incredibly intense hacking scene in the movie Social Network. It probably wouldn’t have looked as cool as it did. That’s kind of true! When you have an idea to a problem and you are coding, using the mouse kind of breaks your momentum. So enter Code Blocks feature of customizable keyboard shortcuts or hotkeys. I won’t post a list to these shortcuts as you can totally modify them. Here’s how you can do that.

Under the Settings drop-down, select Editor. This window shall pop-up on your screen. From the pane on the left, scroll down to Keyboard Shortcuts. There will be a comprehensive list on the right of all the actions that can be performed using keyboard shortcuts. Click on any of them, make a note of the shortcut or customize it according to your preferences.

how to use code blocks - keyboard shortcuts and hotkeys

I don’t like using the Function (F) keys as shortcuts. So I will change the shortcut of compiling a file to Ctrl+9.

When you exit Code Blocks, you will be asked if you’d like to save the Workspace and the Layout. The Layout is the placement of various windows that you may have positioned. And the Workspace refers to the projects you are working on. Saving your Workspace will open your files in the Workspace section the way you leave them. Similarly saving your Layout will retain the arrangement of Windows that you had done last.

Is this everything that we can do in Code Blocks?

Of course not! Enlisting all the things that you can do with Code Blocks would be a tad overkill for this post. More importantly, it would defeat the purpose of not getting overwhelmed by the IDE. We will periodically slide in the more complex set of features, things like installing and/or writing your own plugins and so on, as we progress through this course. But feel free to experiment around. Just assume the curiosity of a toddler and click things and see what happens! Meanwhile, all the tips for using Code Blocks efficiently mentioned above should suffice to get you aware of the IDE and run your first projects confidently.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.