Sie sind auf Seite 1von 17

About CodeBlocks

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:
Open Source! GPLv3, no hidden costs. Cross-platform. Runs on Linux, Mac, Windows (uses wxWidgets). Written in C++. No interpreted languages or proprietary libs needed. Extensible through plugins

Compiler:
Multiple compiler support: o GCC (MingW / GNU GCC) o MSVC++ o Digital Mars o Borland C++ 5.5 o Open Watcom o ...and more Very fast custom build system (no makefiles needed) Support for parallel builds (utilizing your CPU's extra cores)

Multi-target projects
Workspaces to combine multiple projects Inter-project dependencies inside workspace Imports MSVC projects and workspaces (NOTE: assembly code not supported yet) Imports Dev-C++ projects

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 2

Installation of Code Blocks


Step 1: Get the Software
In order to install the Code::Blocks IDE as well as the compiler, pick the one from the accompanying CDROM as per the operating system you are using. You will get the installer application in the folder of appropriate OS on the CDROM. Open the folder and proceed with the instructions. In this section, we are covering the installation of Windows based Code Blocks software. If you are using Mac OS X or Linux, please see Appendix I for installation instructions.

Step 2: Install the Software


Next, open (click on) CodeBlocks install file and the CodeBlocks Setup will begin installing as follows:

Click Next.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 3

Select I Agree

Take the default settings by pressing Next

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 4

Take the default folder to install CodeBlocks to and then select Install.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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 : File New Project .

2. Select "Console application" and press "Go".

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 6

3. Select "C++" as the language you want to use and press "Next".

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 9

8. Edit the code to create your program. As you are working periodically save your changes (main

menu:: File Save 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 ).

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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 :: Build Run (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.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 11

Capturing Program Execution


This section is specific to Windows installations. Most assignments will require you to capture a snapshot of the programs console. Do this at the end of program execution, before closing the console. 1. The console will remain open until you press some key to close it. It should look similar to this:

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.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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.

Saving and Restoring Projects


If you are working on a machine other than your own, youll need to take a copy of your project(s) with you. To do this, simply locate the folder containing your project and copy it to a removable disk, or to your network storage space. You can also archive the project folder as a zip file and email it to yourself. To save space, you may want to remove unnecessary contents before copying or archiving your file. The bin and obj folders inside your project folder can both be safely deleted, since the contents of these folders will be recreated the next time you build your project. The most important files are your source code (any .cpp or .h files that you have written). If you keep these files, it is possible to recreate your project by adding them to a new Code::Blocks project later.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

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 File Open , 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.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 14

Appendix A

Installation of Code::Blocks for Mac OS X and Linux


If you are using the Mac OS, you will need to do the following:
Pre-Install steps:
Here is a step by step instruction file for setting up X11 and XcodeTools for the Mac. 1. Make sure that X11 is installed in your system (check in your Application menu). If not, you'll need to install it from your Mac OS disc. Insert the disc and find the optional packages installer, open it, check the X11 box, and install from that. If you are using 10.1 through 10.3, you can download X11 from Apple. 2. Install Developer Tools: You may be able to skip this step, if you are using Mac OS X 10.4

Tiger and have installed the Developer tools. As a test, open up xterm. You cannot use terminal with Code Blocks. Xterm is part of X11 and that is why you'll need to have X11 installed. Run the command gcc. If you get an error that there are no input files, that means gcc exists and you are good to go. If you get an error saying the command is not found, you need to install the Developer Tools. If you need to install the Developer tools, you can download them from Apple, or you can install them from your Mac OS disc. Locate the Xcode installer on the installation discs and double click to install. Check your discs for Xcode or Developer Tools. Run the installer and install everything other than the documentation: For Mac OS X 10.3, you need to install Xcode Tools version 1.2 or later For Mac OS X 10.4, you need to install Xcode Tools version 2.2 or later For Mac OS X 10.5, you need to install Xcode Tools version 3.1 or later

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 15

Installation for Fedora 8 Linux:


Installing Code::Blocks In a console window, become root and then execute the following command: yum install codeblocks Running Code::Blocks In the Applications Pull Down Menu, go to Programming and click on: Code::Blocks IDE or Open a console window and then execute the following command: codeblocks

Installation for Ubuntu Linux:


Installation Instructions From: http://forums.codeblocks.org/index.php/topic,8208.msg61085.html#msg61085 1) Add the repositories to /etc/apt/sources.list: Open the file with a graphical editor as root. Paste the following line in a terminal:
gksu gedit /etc/apt/sources.list

Paste this at the end:


# deb # deb wx codeblocks http://lgp203.free.fr/ubuntu/ widgets http://apt.wxwidgets.org/ gutsy-wx main gutsy universe

Note: Depending on what version of Ubuntu you are using, in step 1 you may need to replace gutsy with feisty.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 16

2) To make sure your package system trusts these sources. Add their keys. Enter these two lines at the terminal:
wget wget -q -q http://lgp203.free.fr/public.key http://apt.wxwidgets.org/key.asc -O-O| | sudo sudo apt-key apt-key add add -

And update the packages by entering the following lines on the terminal:
sudo sudo apt-get apt-get update 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.

CodeBlocks Tutorial Computer Science C++ by Sumita Arora

Page 17

Das könnte Ihnen auch gefallen