Code::Blocks is an open-source, cross-platform (Windows, Linux, MacOS), and free C/C++ IDE. It supports many compilers, such as GNU GCC (MinGW and Cygwin) and MS Visual C++. It supports interactive debugging (via GNU GDB or MS CDB). CodeBlocks is surprisingly versatile, and is considered by many, much better than the Visual Studio suite. The mother site of CodeBlocks is www.codeblocks.org.
This guide will show you various steps on how to install Code::Blocks in Windows.
Step 1: Download
Goto http://www.codeblocks.org/downloads.
Click "Download the binary release".
Select your operating platform (e.g., Windows 7 / 8 / 10).
Download the installer with GCC Compiler, e.g., codeblocks-20.03mingw-setup.exe
(which includes MinGW's GNU GCC compiler and GNU GDB debugger).
Step 2
Run the downloaded installer. Accept the default options.
Verify the Compiler's and Debugger's Path:
(for CodeBlocks 20.03 for Windows) Goto "Settings" menu ⇒ "Compiler..." ⇒
In "Selected Compiler", choose "GNU GCC Compiler" ⇒ Select tab
"Toolchain Executables" ⇒ Check the "Compiler's Installation Directory".
It shall be set to the "MinGW" sub-directory of the CodeBlocks installation directory,
for example, if CodeBlocks is installed in "C:\Program Files (x86)\CodeBlocks
",
set it to "C:\Program Files (x86)\CodeBlocks\MinGW
".
Similarly, check the debugger's path. Goto "Settings" menu ⇒ "Debugger..." ⇒
Expand "GDB/CDB debugger" ⇒ Select "Default" ⇒ In "Executable path",
provide the full-path name of "gdb.exe
", for example, "
C:\Program Files (x86)\CodeBlocks\MinGW\bin\gdb.exe
".
Step 3
Compiler configuracion to run compiled projects outside of codeblocks
Compiler configuration:
To run compiled projects outside of codeblocks (ie MSDOS) goto "Settings" menu ⇒ "Compiler..." ⇒
In "Compiler Flags Tab", check these options:
Static libgcc [-static -libgcc],
Static libstdc++ [-static -libstdc++],
Static linking [-static]
and pulse OK button.
Read CodeBlocks' Wiki @ http://wiki.codeblocks.org/index.php?title=Main_Page, in particular, "Creating a new project" and "Debug my Program".
To write toy programs (such as few-line simple programming exercises):
// First C++ program to say Hello
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
Save the file as "Hello.cpp
" in your project directory (e.g., "d:\project
").The drawback is you cannot debug program without creating a project.
Other than the few-line toy programs, you shall create a project for each of your application. A project contains related files such as source codes, header files, and relevant resources. Also, under CodeBlocks, you can only debug your program under a project - single-file program (in previous section) debugging is not supported.
HelloProject
". In "Folder to create project in", set to your working directory, e.g., "d:\project
". Accept the default for the rest ⇒ Next.HelloProject
" will be created under "d:\project
", with a project configuration filename of "HelloProject.cbp
". You could later create more projects under this working directory "d:\project
".HelloProject
" ⇒ Expand "Source" node ⇒ Double-click "main.cpp
", which is a template program to say "Hello, world!".You can create more projects. However, the "Build" and "Run" commands are always apply to the active project, which is shown in bold. To activate a project: right-click on the project name ⇒ "Activate Project".
To open an existing project, either:
ProjectName
.cbp
", where ".cbp" stands for CodeBlocks-Project.Although a project may contain many source files, there can only be one main()
function among all the source files. That is, you cannot keep two toy programs (each having a main()
function) in one project (you will get the error "multiple definition of 'main'" when you try to build the project).
You need to create one project for each toy program.
Codeblock, nonetheless, allow you to add files or remove files from a project. The removed files are not deleted and remain in the folder. We could use this feature to write many toy programs under one project. The procedures are as follows:
main.cpp
" automatically.main.cpp
". Build and run the program.myfirst.cpp
". Remove it from the project (because each project can only have one file with main()
), by right-click on "myfirst
.cpp
" ⇒ "remove file from project".main.cpp
". Build and run.myfirst.cpp
" again: First remove "main.cpp
" from the project. Right-click on the project ⇒ Add File... ⇒ Choose "myfirst.cpp
" ⇒ Open ⇒ Check both the "Debug" and "Release" box ⇒ OK. You can now build and run the "myfirst.cpp
".In brief, use the "Add File" and "Remove File" to place your desired toy program file (with the main()
function) under the active project. You can then "Build" the project and "Run" your toy program.
Check:
codeblocks-20.03mingw-setup.exe
").c:\Program Files\codeblocks\MinGW
" suppose that CodeBlocks is installed in "
c:\Program Files\codeblocks
".A previous program is still running. You need to terminate the program by closing the output console window.
Check that you have a main()
function in your function.
Able to use a graphics debugger to debug program is crucial in programming. It could save you countless of hours guessing on what went wrong.
Follow the steps in "Writing C++ Program (with Project)"
to write the following C++ program, to be used for the debugging practice. This program computes and prints
the factorial of n
(=1*2*3*...*n
). The program, however, has a
logical error and produce a wrong answer for n=20
. (It outputs "The Factorial of
20 is -2102132736" - a negative number?!).
/* * Compute the factorial of n, with n=20. * n! = 1*2*3*...*n */ #include <iostream> using namespace std; int main() { int n = 20; // To compute factorial of n int factorial = 1; // Initialize the product to 1 int i = 1; while (i <= n) { factorial = factorial * i; i++; } cout << "The Factorial of " << n << " is " << factorial << endl; return 0; } |
Run the program and observe the output produced:
The Factorial of 20 is -2102132736
Let's use the graphic debugger to debug the program.
Set an initial breakpoint at main()
function by clicking on the "left-margin
" (right-side of the line number) of the line containing main()
. A red circle appears
indicating a breakpoint has been set at that line. A breakpoint suspends program execution for you
to examine the internal states.
From "Debug" menu, select "Start (F8)". The program begins execution but suspends
its execution at the breakpoint, i.e., main()
. An yellow arrow (as shown in the diagram)
appears and points at the main()
, indicating this is the next statement to be executed.
Click the "Debugging Windows" button on the "Debug" toolbar and select " Watches" to enable the "Watch" pane. (You could also do it from the "Debug" menu.)
Click the "Next line" button on the "Debug" toolbar to single-step thru your program. At each of the step, you could examine the internal state of your program, such as the value of the variables (in the "Watches" pane), the outputs produced by your program (in the console), etc.
Single-stepping thru the program and watching the values of the variables and the outputs produced is the ultimate mean in debugging programs - because it is exactly how the computer runs your program!
As mentioned, a breakpoint suspends program execution and let you examine the internal states of the program. To set a breakpoint on a particular line, click the left-margin of that line (or select "Toggle Breakpoint (F5)" from "Debug" menu).
The "Continue" resumes the program execution, up to the next breakpoint, or till the end of the program.
Single-stepping thru a loop with a large count is time-consuming. You could set a breakpoint at the statement immediately outside the loop (e.g., Line 12 of the above program), and issue "Continue" to complete the loop.
Alternatively, you can place the cursor on a particular line, right-click and select "Run-To-Cursor " to resume execution up to this line.
The "Stop" ends the debugging session. Always terminate your current debugging session using "Stop" or "Continue" till the end of the program.
Step-Into and Step-Out: To debug a function, you need to use "Step-Into" to step into the first statement of the function. ("Step-Over" runs the function in a single step without stepping through the statements inside the function.) You could use "Step-Out" to return to the caller, anywhere within the function. Alternatively, you can set a breakpoint inside a function.
Watching a Variable: To add a variable into the "Watches" panel, goto "Debug" ⇒ "Edit Watch..." ⇒ "Add" ⇒ Enter the variable name ⇒ You can select the format, or "watch as array".
REFERENCES & RESOURCES