Professional Documents
Culture Documents
How To Work With CodeBlocks - Tutorial
How To Work With CodeBlocks - Tutorial
Code::Blocks is a free C++ IDE built to meet the most +demanding needs of its users. It is designed to
be very extensible and fully configurable.
Features
Highlights:
Compiler:
Multi-target projects
Imports MSVC projects and workspaces (NOTE: assembly code not supported yet)
Page 1
Introduction
Through the aid of a compiler, a program written in a computer language, such as C++, is turned into
machine code, which is executed on the computer.
The purpose of this manual is to help the student develop the skills to organize program coding and
develop sound techniques for finding and isolating errors.
Here you will learn how to trace the code step by step, so that it becomes clear where the problem is
and why your program does not execute properly. This is called debugging the program. Hand tracing is
useful in helping beginners understand where the bugs are and correct the program appropriately.
Automatic tools have also been developed to help you trace programs that you have written and will be an
important tool as your programs become more complex. This type of tool is called a debugger.
A debugger lets you pause a program, while it is in the middle of running, and watch what is going on.
Some debuggers work as command-line line debuggers, but newer debuggers have a nice graphical user
interface, which is useful in helping you watch variables that you have defined as the program executes.
The graphically- based debugger environment is part of what is called the Integrated Development
Environment (IDE).
A debugger cannot solve your problems for you. It is merely a tool to assist you when programming.
You should first attempt to read over your code and using paper and pencil analyze the code to get an
understanding of what is going on. Once you have gotten an idea of where in your code you have an error,
you can then set the debugger to watch certain variables in your program. Watching your code will show
you step by step how your program is being executed.
The debugger that you will use is part of an Open Source free IDE called Code::Blocks, which we
have found easy to use and is described in these notes. Code::Blocks has a C++ editor and compiler. It
will allow you to create and test your programs from one easy to use application. We hope these notes will
assist you in making programming more enjoyable and help you develop better programming skills.
You may find additional information regarding Code::Blocks at: http://www.codeblocks.org/
A complete manual for Code::Blocks is available here: http://www.codeblocks.org/user-manual
Page 2
Click Next.
Page 3
Select I Agree
Page 4
Take the default folder to install CodeBlocks to and then select Install.
Page 5
Using Code::Blocks
This section explains how to create a console project, which is suitable for basic C++ programs that
use the console (keyboard input and text output) for a user interface. The example screenshots in this
section are from a Windows installation. Macintosh users may see slightly different screens, but the
commands should be identical.
For each new program, youll need to create a project. A project will contain your source code,
generated executables and various configuration files within a single folder.
1. From the Start Here screen, click on Create a new project. You can also start from the main
menu using the command : FileNewProject .
Page 6
3. Select "C++" as the language you want to use and press "Next".
Page 7
4. The next screen asks you to name your project and select a location for your project. Be sure you place the
project in an easy to find location
and give it a meaningful name, so
that you can find it again later.
For this example, the project
has been created in the directory
C:\codeblocks-tutorial, with name
first-project.
5. The next screen sets up the compiler and configuration for the project. Select the GNU GCC Compiler and
be sure that both the Debug and Release check boxes are selected. Press Finish to create your project.
Page 8
6. This will create a new folder for the project within the location folder you selected. You can verify the
location of the project by navigating to the appropriate directory. For example:
When the project is initially created, it will contain two files. The .cbp file is the Code::Blocks project file, which
can be used to reopen the project later. The .cpp file is the C++ program file that you will edit to create your
program.
7. Expand the tree in the Projects tab to the left of the Code::Blocks window and double click on main.cpp
to load the program file into the editor.
Page 9
8. Edit the code to create your program. As you are working periodically save your changes (main
menu:: FileSave all files . You can also save all files with the short cut CTRL-SHIFT-s , or save a single
file with the short cut CTRL-s .
9. Build the project to compile your program into an executable. Use the Build menu and select
Build (or press CTRL-F9 ).
Page 10
10. Check for error messages in the lower window. If there are errors, return to step 8 and correct
them. If there are no errors, proceed to step 11.
11. Run your program. main menu :: BuildRun (or press CTRL-F10 ).
12. If the program runs as expected, youre finished. If it does not run as expected, return to step 8
and correct your program.
Page 11
2. Right-click the mouse anywhere in the black console area. To capture the entire output of the
program, choose Select All . To capture only part of the output, choose Mark, then drag your
mouse over the area you want to copy. In either case, the selected area will turn white.
Page 12
3. Press the Enter key to copy of the selected text to the Windows copy/paste buffer.
4. Return to the edit window for your program. Add a comment at the bottom of your program and
paste the text (CTRL-v) into that comment. Build and run the program to verify that the comment is
correct and then save the file.
Page 13
If you need to return to work on a previous project, start Code::Blocks and load the old project. From
the main menu, select FileOpen , then navigate to your project folder and locate the .cbp file. Select
the .cbp file and press Open and youll be returned to the editing environment for your project. Return
to step 8 in the section Using Code::Blocks and continue working.
Short Alternative
To quickly type and run a simple C++ program, you do the following:
1. Click File New Empty File or press Ctrl+Shift+N
2. Type your code in the blank window that opens.
3. Click File Save File or press Ctrl+S.
4. Type the full name of your program file in double quotes (along with extension .cpp) e.g. to
save a file with name First, save it as First.cpp
5. Now compile and run your program by clicking Build Build and Run or by pressing F9.
Page 14
Appendix A
Install Developer Tools: You may be able to skip this step, if you are using Mac OS X 10.4
Page 15
gedit
/etc/apt/sources.list
codeblocks
deb
#
deb
http://lgp203.free.fr/ubuntu/
wx
gutsy
universe
widgets
http://apt.wxwidgets.org/
gutsy-wx
main
Note: Depending on what version of Ubuntu you are using, in step 1 you
may need to replace gutsy with feisty.
Page 16
2) To make sure your package system trusts these sources. Add their keys.
Enter these two lines at the terminal:
wget
-q
http://lgp203.free.fr/public.key
-O-
sudo
apt-key
add
wget
-q
http://apt.wxwidgets.org/key.asc
-O-
sudo
apt-key
add
And update the packages by entering the following lines on the terminal:
sudo
apt-get
update
sudo
apt-get
upgrade
3) Install Code::Blocks
Enter the following line in the terminal:
sudo
apt-get
codeblocks-contrib
install
libcodeblocks0
codeblocks
libwxsmithlib0
You are able to step 3 whenever you want you to get the latest nightly build.
You should see Code::Blocks in the Programming Languages listing of your programs.
Page 17