0% found this document useful (0 votes)
255 views

CodeBlocks C C++

CodeBlocks is a free open-source C/C++ IDE that works on Windows, Linux, and MacOS. It supports compilers like GCC and MS Visual C++. To install CodeBlocks, download the installer, run it, and verify the compiler and debugger paths. To write programs in CodeBlocks, you can write simple programs without creating a project or write more complex programs by creating a project. You can create multiple projects and debug programs under a project.

Uploaded by

semabay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views

CodeBlocks C C++

CodeBlocks is a free open-source C/C++ IDE that works on Windows, Linux, and MacOS. It supports compilers like GCC and MS Visual C++. To install CodeBlocks, download the installer, run it, and verify the compiler and debugger paths. To write programs in CodeBlocks, you can write simple programs without creating a project or write more complex programs by creating a project. You can create multiple projects and debug programs under a project.

Uploaded by

semabay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

How to install CodeBlocks and Get Started with C/C++ Programming https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlock...

yet another insignificant programming notes... | HOME

TABLE OF CONTENTS (HIDE)


1. How to Install CodeBlocks 13.12

CodeBlocks for C/C++ 2. Wri�ng C/C++ Programs in CodeBlocks


2.1 Wri�ng Toy Programs (without Crea�ng a Project)

Programming
2.2 Wri�ng Programs (under Project)
2.3 Wri�ng Many Toy Programs under ONE Project
3. CodeBlocks' Common Errors

How To Install and Get Started


4. Debugging C/C++ Program in CodeBlocks
5. Tips and Tweaks

CodeBlocks is an open-source, cross-pla�orm (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 interac�ve debugging (via GNU GDB or MS CDB). CodeBlocks is surprisingly versa�le, and in my opinion, much be�er than the
Visual Studio suite. The mother site of CodeBlocks is www.codeblocks.org.

1. How to Install CodeBlocks 13.12

Step 1: Download
Goto h�p://www.codeblocks.org/downloads. Click "Download the binary release". Select your opera�ng pla�orm (e.g., Windows 2000/XP/Vista/7). Download
the installer with GCC Compiler, e.g., codeblocks-13.12mingw-setup.exe (98 MB) (which includes MinGW's GNU GCC compiler and GNU GDB
debugger).

Step 2: Install
Run the downloaded installer. Accept the default op�ons.

Verify the Compiler's and Debugger's Path : (For CodeBlocks 13.12 For Windows) Goto "Se�ngs" menu ⇒ "Compiler..." ⇒ In "Selected Compiler",
choose "GNU GCC Compiler" ⇒ Select tab "Toolchain Executables" ⇒ Check the "Compiler's Installa�on Directory". It shall be set to the "MinGW" sub-directory
of the CodeBlocks installa�on directory, for example, suppose that CodeBlocks is installed in "c:\Program Files\codeblocks", set it to "c:\Program
Files\codeblocks\MinGW".

Similarly, check the debugger's path. Goto "Se�ngs" menu ⇒ "Debugger..." ⇒ Expand "GDB/CDB debugger" ⇒ Select "Default" ⇒ In "Executable path", provide
the full-path name of "gdb.exe", for example, "c:\Program Files\codeblocks\MinGW\bin\gdb.exe".

Notes For CodeBlocks 13.12 For Windows (Jan 2014)

I encountered problem running debugger with CodeBlocks 13.12 bundled with MinGW (gcc v4.7.1 and gdb 7.5).

I resolved by installing the latast MinGW (gcc 4.8.1, gdb 7.6.1) separately (See "How to install MinGW"), and configured the compiler's and debugger's
path to the installed MinGW as in the above step.

Alterna�vely, consider using Eclipse or Netbeans with Cygwin or MinGW GNU GCC compiler.

2. Writing C/C++ Programs in CodeBlocks


Read:
1. CodeBlocks' Wiki @ h�p://wiki.codeblocks.org/index.php?�tle=Main_Page, in par�cular, "Crea�ng a new project" and "Debug my Program".

2.1 Writing Toy Programs (without Creating a Project)


To write toy programs (such as few-line simple programming exercises):
1. File ⇒ New ⇒ Empty File.
2. Enter (copy and paste) the following codes:

// 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").

1 of 5 22/03/2020, 19:57
How to install CodeBlocks and Get Started with C/C++ Programming https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlock...

3. Build (Compile and Link): Select "Build" menu ⇒ Build (Ctrl-F9).


4. Run: Select "Build" menu ⇒ Run (Ctrl-F10).

The drawback is you cannot debug program without crea�ng a project.

Read "CodeBlocks' Common Errors" if ....

2.2 Writing Programs (under Project)


Other than the few-line toy programs, you shall create a project for each of your applica�on. 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 sec�on) debugging is not
supported.
1. File ⇒ New ⇒ Project... ⇒ Console Applica�on ⇒ Go.
2. The "Console Applica�on" wizard appears:
a. Next
b. Select "C++" ⇒ Next.
c. In "Project Title", enter "HelloProject". In "Folder to create project in", set to your working directory, e.g., "d:\project". Accept the default
for the rest ⇒ Next.
A project directory "HelloProject" will be created under "d:\project", with a project configura�on filename of "HelloProject.cbp".
You could later create more projects under this working directory "d:\project".
d. In "Compiler" field, accept the defaults of "GNU GCC Compiler" ⇒ Finish.
3. Under the "Management" pane ⇒ Choose "Projects" tab ⇒ Expand the project node "HelloProject" ⇒ Expand "Source" node ⇒ Double-click
"main.cpp", which is a template program to say "Hello, world!".
4. To build the program, select "Build" menu ⇒ Build.
5. To run the program, select "Build" menu ⇒ Run.
6. To create more source file or header file under the project:
a. File ⇒ New File... ⇒ Select C/C++ source or C/C++ header.
b. C++ ⇒ Next.
c. In "Filename with full path" ⇒ Click the "Navigate" (...) bu�on to navigate to the project directory and enter the new file name. Check both the
"Debug" and "Release" boxes (or "All") ⇒ Finish.

Set Active Project


You can create more projects. However, the "Build" and "Run" commands are always apply to the ac�ve project, which is shown in bold. To ac�vate a project:
right-click on the project name ⇒ "Ac�vate Project".

Open an Existing Project


To open an exis�ng project, either:
1. From "File" menu ⇒ "Recent Projects" ⇒ Choose the desired project; or
2. From "File" menu ⇒ "Open..." ⇒ Navigate to your project directory ⇒ Choose "ProjectName.cbp", where ".cbp" stands for CodeBlocks-Project.

Read "CodeBlocks' Common Errors" if ....

2.3 Writing Many Toy Programs under ONE Project


Although a project may contain many source files, there can only be one main() func�on among all the source files. That is, you cannot keep two toy
programs (each having a main() func�on) in one project (you will get the error "mul�ple defini�on of 'main'" when you try to build the project). You need to
create one project for each toy program. This is clumsy!

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:
1. Create a C/C++ project called "ToyProgramProject" (read previous sec�on on how to create a project). You shall get a "main.cpp" automa�cally.
2. Write your toy program on "main.cpp". Build and run the program.
3. To write another program: select "File" ⇒ "Save File as" ⇒ enter a program name such as "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".
4. Con�nue to write your second toy program on "main.cpp". Build and run.
5. Repeat Step 3 and 4 for another toy program.
6. Suppose that you wish to 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() func�on) under the ac�ve project. You can then "Build"
the project and "Run" your toy program.

2 of 5 22/03/2020, 19:57
How to install CodeBlocks and Get Started with C/C++ Programming https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlock...

3. CodeBlocks' Common Errors

Cannot Compile any C/C++ Program after Installing CodeBlocks


Check:
1. You downloaded the CodeBlocks with "MinGW GNU C/C++ Compiler" (e.g., "codeblocks-10.05mingw-setup.exe").
2. Goto "Se�ngs" menu ⇒ "Compiler..." ⇒ Select tab "Toolchain Executables" ⇒ Check the "Compiler's Installa�on Directory". It shall be set to the "MinGW"
sub-directory of the CodeBlocks installa�on directory, e.g., "c:\Program Files\codeblocks\MinGW" suppose that CodeBlocks is installed in
"c:\Program Files\codeblocks".

Cannot Build or Run Program - Build/Run Buttons and Menu-Items are Grey and Not Selectable
A previous program is s�ll running. You need to terminate the program by closing the output console window.

Error: undefined reference to `WinMain@16'


Check that you have a main() func�on in your func�on. Check your spelling of main!

(For C Programs) System Error: "xxx.exe has stopped working"


Check your scanf() func�on. You probably omi�ed the '&' before the variable name.

4. Debugging C/C++ Program in CodeBlocks


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.

Step 0: Write a C++ Program


Follow the steps in "Wri�ng C++ Program (with Project)" to write the following C++ program, to be used for the debugging prac�ce. 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 nega�ve number?!).

1 /*
2 * Compute the factorial of n, with n=20.
3 * n! = 1*2*3*...*n
4 */
5 #include <iostream>
6 using namespace std;
7
8 int main() {
9 int n = 20; // To compute factorial of n
10 int factorial = 1; // Initialize the product to 1
11
12 int i = 1;
13 while (i <= n) {
14 factorial = factorial * i;
15 i++;
16 }
17 cout << "The Factorial of " << n << " is " << factorial << endl;
18 return 0;
19 }

Run the program and observe the output produced:

The Factorial of 20 is -2102132736

Let's use the graphic debugger to debug the program.

Step 1: Set an Initial Breakpoint


Set an ini�al breakpoint at main() func�on by clicking on the "le�-margin"
(right-side of the line number) of the line containing main(). A red circle
appears indica�ng a breakpoint has been set at that line. A breakpoint
suspends program execu�on for you to examine the internal states.

Step 2: Start Debugging


From "Debug" menu, select "Start (F8)". The program begins execu�on but suspends its execu�on at the
breakpoint, i.e., main(). An yellow arrow (as shown in the diagram) appears and points at the main(),
indica�ng this is the next statement to be executed.

3 of 5 22/03/2020, 19:57
How to install CodeBlocks and Get Started with C/C++ Programming https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlock...

Step 3: Single-Step and Watch the Variables and Outputs


Click the "Debugging Windows" bu�on 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" bu�on 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 ul�mate mean in
debugging programs - because it is exactly how the computer
runs your program!

Step 4: Breakpoint, Run-To-Cursor, Continue and Stop


As men�oned, a breakpoint suspends program execu�on and let you examine the
internal states of the program. To set a breakpoint on a par�cular line, click the le�-
margin of that line (or select "Toggle Breakpoint (F5)" from "Debug" menu).

The "Con�nue" resumes the program execu�on, up to the next breakpoint, or �ll the
end of the program.

Single-stepping thru a loop with a large count is �me-consuming. You could set a
breakpoint at the statement immediately outside the loop (e.g., Line 12 of the above
program), and issue "Con�nue" to complete the loop.

Alterna�vely, you can place the cursor on a par�cular line, right-click and select "Run-To-Cursor" to resume execu�on up to this line.

The "Stop" ends the debugging session. Always terminate your current debugging session using "Stop" or "Con�nue" �ll the end of the program.

Important: I can's stress more that mastering the use of debugger is crucial in programming. Explore the features provided by the debuggers.

Other Debugging Features


Step-Into and Step-Out: To debug a func�on, you need to use "Step-Into" to step into the first statement of the func�on. ("Step-Over" runs the func�on in
a single step without stepping through the statements inside the func�on.) You could use "Step-Out" to return to the caller, anywhere within the func�on.
Alterna�vely, you can set a breakpoint inside a func�on.

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".

5. Tips and Tweaks


1. Re-format Source Code: Right-click on the source file ⇒ Format this file (AStyle). [Source forma�ng feature is provided by a plugin called AStyle,
which can be accessed via menu "Plugins" ⇒ "Source Code Forma�er (AStyle)". To configure the forma�ng styles, goto "Se�ngs" ⇒ "Editor..." ⇒ "Source
Forma�er".]

2. Auto-Complete: type the ini�al le�ers of a keyword/iden�fier and press Ctrl-space to list the available op�ons.

3. Abbreviation: e.g., type "for" and press control-J to get the skeleton of for-loop. The abbrevia�on list can be configured in "Se�ngs" menu ⇒
"Editor..." ⇒ "Abbrevia�ons".

4. Zoom in/out on Editor Panel: Either:


a. From "Edit" menu ⇒ "Special Commands" ⇒ "Zoom" ⇒ "In", "Out", or "Reset", or
b. Hold the control key and zoom in/out via the mouse scroll wheel, or
c. Use keyboard shortcut: control number-pad + (zoom in), control number-pad / (zoom out).

5. Configure Editor: Right-click on the editor panel ⇒ "Configure editor...".


a. Source Forma�er: You can choose the "Java" style.
b. [TODO] more
6. [How to link to API documenta�on?]

REFERENCES & RESOURCES


1. CodeBlocks Mother Site @ h�p://www.codeblocks.org/.
2. CodeBlocks' Wiki @ h�p://wiki.codeblocks.org/index.php?�tle=Main_Page.
3. CodeBlocks' User Manual @ h�p://www.codeblocks.org/user-manual.

4 of 5 22/03/2020, 19:57
How to install CodeBlocks and Get Started with C/C++ Programming https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlock...

Latest version tested: CodeBlocks 13.12


Last modified: February, 2014

Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan ([email protected]) | HOME

5 of 5 22/03/2020, 19:57

You might also like