How To Work With Code Blocks - Tutorial
How To Work With Code Blocks - Tutorial
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
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
Page 2
Click Next.
Page 3
Select I Agree
Page 4
Take the default folder to install CodeBlocks to and then select Install.
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 .
Page 6
3. Select "C++" as the language you want to use and press "Next".
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.
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.
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 ).
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.
Page 11
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.
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.
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.
Page 14
Appendix A
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
Page 15
Note: Depending on what version of Ubuntu you are using, in step 1 you may need to replace gutsy with feisty.
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
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.
Page 17