Tutorial CodeBlocks
Tutorial CodeBlocks
What is Code::Blocks?
To develop programs, we will use software that allows:
• to edit code (type the program)
• to use compilation tools to create the executable
• to debug the program during its execution
Such a program is called an IDE: Integrated Development Environment.
There are many programs for developing in the C language.
Code::Blocks is the IDE we have chosen to use for your learning of the
programming for the following reasons:
• it is free and open source
• it is multi-platform (version for Windows or for Linux)
• it is easy to install
• It is of reasonable size (installer <35MB with compilation tools)
• it is easy to handle
• he is efficient
Code::Blocks is capable of generating applications written in C or C++ under Windows.
console mode or not). To be able to use Code::Blocks, you need to associate a compiler with it. Under
Windows, we will use MinGW. MinGW (Minimalist GNU* forWindows) uses the tools of
free compilations. These tools are recognized as being among the most efficient. They are
available on almost all market platforms.
*
The GNU project, launched since 1984, develops a free and complete operating system similar to Unix.
Installer Code::Blocks
Once you have downloaded the installer, you can run it. You then need to
go through the following steps:
• home screen and indication of the software version update date
• Choice of the installation directory for Code::Blocks
• Choice of MinGW installation directory (preferably do not modify)
• name of the folder for creating shortcuts in the Start menu
• creation or not of the menu and an icon on the desktop to launch the program
• launch of the installation
• progress of the installation
• end of installation
First program
Create a project
A project contains all the elements necessary to compile a program.
You can create a new project either by the menu File → New → Project...
You can also simply click on Create a new project in the middle of the homepage.
A dialog box opens for you. So all the project files will be placed
allow to choose the type of project that you in the directory
wish to create. First of all, you My Documents\codeblocks\FirstProg
only create console type projects For the project name, it is preferable to
Application. Sélectionnez doncConsole use only letters and numbers and
Application then click on Go. to avoid all special characters, in
particularly the spaces.
You then need to choose the compiler, it
must be automatically selected at GNU
GCC Compiler, and the creation of a version of
debugging (Debug) and a final version
(Release) of your program. Even if for
for exercises we can settle for one version
debugging does not change anything to the options
proposed.
The toolbarCompiler:
The final version produces a smaller and generally more efficient executable file, but it
does not allow debugging. The compiled source files are stored in the subdirectory obj
project. The executable program is located in the subdirectory bin\Debug for the version of
debugging and in the subdirectory bin\Release for the final version. Under Windows, the name of the
The program corresponds to the project name with the extension exe.
Compilation error
Errors during the project compilation are recorded in the Build messages tab.
messageWindow. A simple click on the error sends the cursor to the code where the error is.
detected. A red marker in the margin of the code indicates the line in question. Warning, remember
In C language, a missing semicolon at the end of a line leads to an error on that line.
Next. As an example, let's deliberately create two errors in the previous program.
Breakpoints
To set or remove a breakpoint in the program, you need to click in the
Merge just after the line number. A red dot should appear. You can also use the
menuDebug → Toggle breakpointou la touche F5. Lors de l'exécution en mode débogage, le
the program will stop at the breakpoints.
It is possible to disable a breakpoint (without deleting it), to associate it with a
number of passages before stopping or a logical condition to stop. One click
Right-clicking on the breakpoint allows access to a breakpoint editing menu (Edit
(breakpoint). A dialog box opens and one can choose to ignore a
certain number of passages before taking into account the breakpoint or not
stop only for a particular logical condition expressed in C language.
During a break in debug mode, a yellow arrow indicates the progress of the program execution.
You can open this window via the Debug menu → Debugging windows → Watches.
You can also use the Debugger toolbar and the window opening icon for debugging.
The Watches window by default includes two lists, one for local variables and one for the
arguments of the functions. The variables in these lists are added and removed in a manner
automatic depending on the progress of the program. A stop in the following function:
double fct(double k)
{
double y;
y = k * x;
return y;
}
A right-click on a variable in the code during a debugging session allows you to obtain a
menu to add the visualization of the variable.
Other features
Install a help file
It is convenient to quickly obtain 6. Check the option
help on library functions This is the default help file (shortcut: F1)
C language standard. To do this, it is necessary to activate the use of the F1 key.
Associate a help file with a press on 7. Finish by clicking OK.
F1 key when the cursor is placed on
a function.
Go to the menu
Settings → Environment...
2. In the window that opens, do
scroll the left part until you find
Help fileset click on Help files.
3. Click on the Add button and enter a
name for the help file (for example
GCC Libraries).
4. Click on Yes to browse To check the operation, hover over
browse the file structure. the main function and press F1. A help window
5. Search for the file [Link] which should open to provide you with information about the
find at the location main function in C language.
C:\Program Files\CodeBlocks\[Link] Of course, this help is in the language of Shakespeare!
for a standard installation.
Automatically format your code
The C language imposes no constraints on code writing. For you who write in C language
like a pig, Code::Blocks integrates an automatic code formatting tool: AStyle.
To use it, simply go to the menu Plugins → Source code formatter (AStyle).
You can configure the code formatting by going to the menu Settings → Editor... then in
selecting on the left of the Source formatting window.
Before AStyle After AStyle
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
Code::Blocks has many other interesting features that you will discover with use...
As the developers of Code::Blocks write on their site:
We hope you enjoy using Code::Blocks!
A big thank you to the teams developing the open-source tools we use.