Sie sind auf Seite 1von 7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

yet another insignificant programming notes... | HOME

TABLE OF CONTENTS (HIDE)

CodeBlocks for C/C++ Programming


How To Install and Get Started

1. How to Install CodeBlocks 10.05 2. Writing C/C++ Programs in CodeBlocks

2.1 Writing Toy Programs (without Creating a P 2.2 Writing Programs (under Project) 2.3 Writing Many Toy Programs under ONE Pr

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

CodeBlocks is an open-source, cross-platform (Windows, Linux, Mac), and free C/C++ IDE. CodeBlock supports many compilers, such as GCC (MingGW and GNU GCC) and MS Visual C++. It supports interactive debugging (via GNU GDB or MS CDB). CodeBlocks is surprisingly versatile, and in my opinion, much better than the Visual Studio suite. The mother site of CodeBlocks is www.codeblocks.org.

1. How to Install CodeBlocks 10.05


Step 1: Download
Goto http://www.codeblocks.org/downloads. Click "Download the binary release". Select your operating platform, e.g., Windows 2000/XP/Vista/7. Download the installer, e.g., c o d e b l o c k s 1 0 . 0 5 m i n g w s e t u p . e x e(74 MB) (which includes MingGW's GCC compiler and GDB debugger).

Step 2: Install
Run the downloaded installer. Accept the default options.

2. Writing C/C++ Programs in CodeBlocks


Read:
www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html 1/7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

1. CodeBlocks' Wiki @ http://wiki.codeblocks.org/index.php?title=Main_Page, "Creating a new project" and "Debug my Program".

in

particular,

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:
/ /F i r s tC + +p r o g r a mt os a yH e l l o # i n c l u d e< i o s t r e a m > u s i n gn a m e s p a c es t d ; i n tm a i n ( ){ c o u t< <" H e l l o ,w o r l d ! "< <e n d l ; r e t u r n0 ; }

Save the file as "H e l l o . c p p " in your project directory (e.g., "d : \ p r o j e c t "). 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 creating a project.

2.2 Writing Programs (under 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. 1. File New Project... Console Application Go. 2. The "Console Application" wizard appears: a. Next b. Select "C++" Next. c. In "Project Title", enter "H e l l o P r o j e c t ". In "Folder to create project in", set to your working directory, e.g., "d : \ p r o j e c t ". Accept the default for the rest Next. A project directory "H e l l o P r o j e c t " will be created under "d : \ p r o j e c t ", with a project configuration filename of "H e l l o P r o j e c t . c b p ". You could later create more projects under this working directory "d : \ p r o j e c t ". d. In "Compiler" field, accept the defaults of "GNU GCC Compiler" Finish. 3. Under the "Management" pane Choose "Projects" tab Expand the project node "H e l l o P r o j e c t " Expand "Source" node Double-click "m a i n . c p p ", which is a template program to say "Hello, world!".
www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html 2/7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

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" (...) button 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 active project, which is shown in bold. To activate a project: right-click on the project name "Activate Project".

Open an Existing Project


To open an existing project, either: 1. From "File" menu "Recent Projects" Choose the desired project; or 2. From "File" menu "Open..." Navigate to your project directory "P r o j e c t N a m e . c b p ", where ".cbp" stands for CodeBlocks-Project. Choose

2.3 Writing Many Toy Programs under ONE Project


Although a project may contain many source files, there can only be one m a i n ( )function among all the source files. That is, you cannot keep two toy programs (each having a m a i n ( )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. 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 section on how to create a project). You shall get a "m a i n . c p p " automatically. 2. To write another toy program, remove "m a i n . c p p " from the project: right-click on "m a i n . c p p " "remove file from project". 3. Add a new file into the project: "File" menu "New" "File..." "C/C++ Source" "Go" Next "C++" Next. a. In "Filename with full path", click the "Navigate" (...) button, enter the filename (e.g., H e l l o . c p p ) Save. b. In "Build targets", select "All" (both "Debug" and "Release" Finish. 4. Enter your source code.
www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html 3/7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

5. Build and run the program. 6. Check your project directory, which shall show that both the m a i n . c p pand H e l l o . c p pare present. 7. Suppose that you wish to run "m a i n . c p p " again: First remove "H e l l o . c p p " from the project. Right-click on the project Add File... Choose "m a i n . c p p " Open Check both the "Debug" and "Release" box OK. You can now build and run the "m a i n . c p p ". In brief, use the "Add File" and "Remove File" to place your desired toy program file (with the m a i n ( ) function) under the active project. You can then "Build" the project and "Run" your toy program.

3. 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 "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 = 2 0 . (It outputs "The Factorial of 20 is -2102132736" - a negative number?!).
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 / * *C o m p u t et h ef a c t o r i a lo fn ,w i t hn = 2 0 . * n !=1 * 2 * 3 * . . . * n * / # i n c l u d e< i o s t r e a m > u s i n gn a m e s p a c es t d ; i n tm a i n ( ){ i n tn=2 0 ; / /T oc o m p u t ef a c t o r i a lo fn i n tf a c t o r i a l=1 ; / /I n i t i a l i z et h ep r o d u c tt o1 i n ti=1 ; w h i l e( i< =n ){ f a c t o r i a l=f a c t o r i a l*i ; i + + ; } c o u t< <" T h eF a c t o r i a lo f"< <n< <"i s"< <f a c t o r i a l< <e n d l ; r e t u r n0 ; }

Run the program and observe the output produced:


T h eF a c t o r i a lo f2 0i s2 1 0 2 1 3 2 7 3 6

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

www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html

4/7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

Step 1: Set an Initial Breakpoint


Set an initial breakpoint at m a i n ( )function by clicking on the "left-margin" (right-side of the line number) of the line containing m a i n ( ) . 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.

Step 2: Start Debugging


From "Debug" menu, select "Start (F8)". The program begins execution but suspends its execution at the breakpoint, i.e., m a i n ( ) . An yellow arrow (as shown in the diagram) appears and points at the m a i n ( ) , indicating this is the next statement to be executed.

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


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 singlestep thru your program. At each of the step, you could examine the internal state of your program, such as the value of the variables (by expanding the "Local variables" node 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
www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html 5/7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

runs your program!

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


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

4. Tips and Tweaks


1. Re-format Source Code: Right-click on the source file Format this file. 2. Auto-Complete: type the initial letters of a keyword/identifier and press control-space to list the available options. 3. Abbreviation: e.g., type "for" and press control-J to get the skeleton of for-loop. The abbreviation list can be configured in "Settings" menu "Editor..." "Abbreviations". 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).
www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html 6/7

5/13/12

How to install CodeBlocks and Get Started with C/C++ Programming

5. Configure Editor: Right-click on the editor panel "Configure editor...". a. Source Formatter: You can choose the "Java" style. b. [TODO] more 6. [How to link to API documentation?]

REFERENCES & RESOURCES


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

Latest version tested: CodeBlocks 10.05 Last modified: January, 2012

Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan (ehchua@ntu.edu.sg) | HOME

www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html

7/7

Das könnte Ihnen auch gefallen