0% found this document useful (0 votes)
35 views6 pages

Tutorial CodeBlocks

Code::Blocks is an open-source Integrated Development Environment (IDE) for programming in C and C++, chosen for its ease of use, efficiency, and multi-platform support. The tutorial covers installation, project creation, building and executing programs, debugging, and additional features like automatic code formatting and help file integration. Users are guided through the steps to set up their environment and troubleshoot common issues during programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Tutorial CodeBlocks

Code::Blocks is an open-source Integrated Development Environment (IDE) for programming in C and C++, chosen for its ease of use, efficiency, and multi-platform support. The tutorial covers installation, project creation, building and executing programs, debugging, and additional features like automatic code formatting and help file integration. Users are guided through the steps to set up their environment and troubleshoot common issues during programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TutorialCode::Blocks IUT of Cachan - GEII2

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.

Download the Code::Blocks installer


The latest official version of Code::Blocks is from October 25, 2005. It is version 1.0rc2.
Do not install this version at all. Since January 2, 2006, a new version of
Code::Blocks is published almost every day. These daily versions are called nightly.
builds. To spare you a tedious installation of Code::Blocks from MinGW, we have
grouped everything you will need to program under Windows in the same file
installation with the updated versions in June 2007. You can download this program at
the following address:
[Link]
where to go:
[Link]
then in the category of Sciences, Technologies
chercherLangage C – PROGC – Guinand Yves – french
and finally go to Documents

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

1/6 YG 2007 – v1.0


Code::Blocks Tutorial IUT of Cachan - GEII2

First run of Code::Blocks


On the first execution of Code::Blocks,
if multiple compilers are installed on your
machine, you have to choose the one you will
use by default. The compiler installed with
Code::Blocks and GNU GCC Compiler.

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.

You now need to choose the project name.


and its backup directory.

It remains to choose a development of


program in C language.

Code::Blocks automatically places all the


project files in a directory that bears the
project name. In the example below, the
the project directory is
My Documents\codeblocks
and the name of the project is
PremierProg A click on Finish will now create the project.

2/6 YG 2007 – v1.0


Tutorial Code::Blocks Cachan IUT - GEII2

In the directory My Documents\codeblocks\FirstProg, two files have been created:


• [Link] project file whose format is specific to Code::Blocks with extension
cbppourCode::BlocksProject.
• [Link] in C language.

Build and execute a project


You can edit the file [Link] by going left in the Management window in Sources
→ main.c. This file contains the main function, the main function of the program.

The file main.c contains


the instructions to display
Hello world! at runtime.
Pour pouvoir exécuter le
program, you need everything
first build the
project. This operation can
be carried out either by the menu
Build, either by the bar
of tools that one
can enable or disable
parView → Toolbars →
Compiler.

The toolbarCompiler:

Project Construction (Build)


Exécution du projet (Run)
Construction, then execution of the project (Build and run)
Rebuild
Terminate the ongoing execution (Abort)
Choice of target type, debug or release version

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.

We obtain the display of the result of a build.


in the Build log tab of the window
Messages.

If we launch the program (Run), a window


opens and displays the result of the execution.

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.

3/6 YG 2007 - v1.0


Code::Blocks Tutorial IUT of Cachan – GEII2

Let's write sprint in place of


printfet let's remove the point-
comma after lereturn 0. During
duBuild, we then get
the display opposite:
The absence of a semicolon
appears as an error.
At compilation, the use of
the unknown function print(
the printf function generates a
warning.
She would have triggered a
error in link editing.

Use the debugger


Debugger commands
It is possible to use the debugger either from the Debug menu or from the toolbar.
Debugger. You can enable or disable this toolbar by View → Toolbars → Debugger.

The Debugger toolbar:

Run in debugger mode (Debug/Continue)


Attention, if no breakpoint is set,
the program runs normally.
Run to cursor
Execute a line without entering the functions (Next line)
Execute an instruction in assembly (Next instruction)
Execute a line by stepping into functions
Execute until the end of the current function (Step out)
Terminate the ongoing execution (Abort)
Debug Window Opening Menu
Information menu

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.

Visualize the state of the variables (Watches)


During debugging mode, Code::Blocks automatically analyzes the values of
local variables and function arguments. This information can be found in the window
Watches.

4/6 YG 2007 - v1.0


TutorialCode::Blocks IUT of Cachan - GEII2

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;
}

where is a global variable gives the above display


in the Watches window (x was added manually).

A right click in the Watches window allows access to a


menu to add a variable (Add watch).
A right-click on a variable in the Watches window
allows you to change its value (Change value) or to modify
its representation (Edit watch).

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.

Example of a debugging session


The program is stopped by a breakpoint with condition:

5/6 YG 2007 – v1.0


Tutorial Code::Blocks IUT of Cachan - GEII2

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>

double x = 1.0; double x = 1.0;

void fct(void); void fct(void);

int main() int main()


{ {
int i; int i;

printf("Hello world!\n"); printf("Hello world!\n");


for (i=1; i<10; i++) { for (i=1; i<10; i++)
fct(); {
2^%d = %lf fct();
} printf("2^%d = %lf\n", i, x);
return 0; }
} return 0;
}
void fct(void){
x = 2 * x; void fct(void)
} {
x = 2 * x;
}

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.

6/6 YG 2007 – v1.0

You might also like