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

C++ Env Setting and Write A Program

The document discusses how to write and run programs in C++ using the CodeBlocks IDE. It describes how to install and set up CodeBlocks, how to create new projects and files to write code, and how to build, run, and debug programs. Key steps include downloading and installing CodeBlocks, creating a new project, adding source code files to the project, building and running programs from the Build menu, and debugging programs by running under a project.

Uploaded by

Zabih keraam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

C++ Env Setting and Write A Program

The document discusses how to write and run programs in C++ using the CodeBlocks IDE. It describes how to install and set up CodeBlocks, how to create new projects and files to write code, and how to build, run, and debug programs. Key steps include downloading and installing CodeBlocks, creating a new project, adding source code files to the project, building and running programs from the Build menu, and debugging programs by running under a project.

Uploaded by

Zabih keraam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Write a Program

Chapter 2. Write a Program


Objective
› You will learn how to write and execute a program in C+
+
› Get familiar with C++ IDEs
CodeBlocks IDE
› To write a program and editor is required.
› CodeBlocks is an open-source, cross-platform
(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 interactive
debugging (via GNU GDB or MS CDB).
› Dev-C++ is a free full-featured integrated development
environment distributed under the GNU General Public
License for programming in C and C++.
 How to Install and setup CodeBlocks
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 with GCC Compiler,
e.g., codeblocks-13.12mingw-setup.exe (98 MB) (which
includes MinGW's GNU GCC compiler and GNU GDB
debugger).
 How to Install and setup CodeBlocks
› Step 2: Install
› Run the downloaded installer. Accept the default options.
 How to Install and setup CodeBlocks
Step 4: Compile the program
Verify the Compiler's and Debugger's Path: (For CodeBlocks
13.12 For Windows)
Goto "Settings" menu ⇒ "Compiler..." ⇒
In "Selected Compiler", choose "GNU GCC Compiler" ⇒ Select
tab "Toolchain Executables" ⇒ Check the "Compiler's
Installation Directory".
It shall be set to the "MinGW" sub-directory of the CodeBlocks
installation directory, for example, suppose that CodeBlocks is
installed in "c:\Program Files\codeblocks", set it to "c:\
Program Files\codeblocks\MinGW".
 How to Install and setup CodeBlocks
› Similarly, check the debugger's path. Goto "Settings"
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".
Writing C/C++ Programs in CodeBlocks
› 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").
Writing C/C++ Programs in CodeBlocks
› Build (Compile and Link): Select "Build" menu ⇒ Build
(Ctrl-F9).
› Run: Select "Build" menu ⇒ Run (Ctrl-F10).
› The drawback is you cannot debug program without
creating a project.
Writing Programs (under Project)
› 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.
Writing Programs (under Project)
› File ⇒ New ⇒ Project... ⇒ Console Application ⇒ Go.
› The "Console Application" 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".
d) Accept the default for the rest ⇒ Next.
A project directory "HelloProject" will be created under "d:\project", with
a project configuration filename of "HelloProject.cbp". You could later
create more projects under this working directory "d:\project".
e) In "Compiler" field, accept the defaults of "GNU GCC Compiler" ⇒
Finish.
Writing Programs (under Project)

1.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!".
2.To build the program, select "Build" menu ⇒ Build.
3.To run the program, select "Build" menu ⇒ Run.
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 ⇒ Choose "ProjectName.cbp", where
".cbp" stands for CodeBlocks-Project.
Practice
› Write a program to print your name.
Home Work
› Write a program to add two numbers. The output
should be such below.
› The addition of 3 and 2 is : 5
Questions?

Thank You

You might also like