CodeBlocks C C++
CodeBlocks C C++
Programming
2.2 Wri�ng Programs (under Project)
2.3 Wri�ng Many Toy Programs under ONE Project
3. CodeBlocks' Common Errors
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.
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".
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.
int main() {
cout << "Hello, world!" << endl;
return 0;
}
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...
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...
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.
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 }
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...
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.
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".
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 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...
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan ([email protected]) | HOME
5 of 5 22/03/2020, 19:57