Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views4 pages

Experiment: Lab No 01 Understanding The Codeblocks For C/C++ Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

UNIVERSITY OF POONCH RAWALAKOT

LAB EXPERIMENT NO 01

Understanding the CodeBlocks for C/C++ Programming:

Introduction:
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). CodeBlocks is surprisingly
versatile, and in my opinion, much better than the Visual Studio suite. The mother site of
CodeBlocks is www.codeblocks.org.

How to Install CodeBlocks:


Step 1: Download:
Goto http://www. https://www.fosshub.com/Code-Blocks.html. 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 setup.exe (145.4 MB) (which includes MinGW's
GNU GCC compiler and GNU GDB debugger).

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

   Writing C/C++ Programs in CodeBlocks 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.
1. File ⇒ New ⇒ Project... ⇒ Console Application ⇒ Go.
2. The "Console Application" wizard appears:
1. Next
2. Select "C++" ⇒ Next.
3. In "Project Title", enter "Hello Project". In "Folder to create project in", set to
your working directory, e.g., "d:\project". 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".
4. In "Compiler" field, accept the defaults of "GNU GCC Compiler" ⇒ Finish.
b. 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!".
c. To build the program, select "Build" menu ⇒ Build.
d. To run the program, select "Build" menu ⇒ Run.

Data Structures and Algorithms (CS-245) 1


UNIVERSITY OF POONCH RAWALAKOT

e. To create more source file or header file under the project:


1. File ⇒ New File... ⇒ Select C/C++ source or C/C++ header.
2. C++ ⇒ Next.
3. 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 ⇒ Choose
"ProjectName.cbp", where ".cbp" stands for CodeBlocks-Proje

Writing Many Toy Programs under ONE Project:


Although a project may contain many source files, there can only be one main() function among
all the source files. That is, you cannot keep two toy programs (each having a main() 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 "main.cpp" automatically.
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. Continue 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() function) under the active project. You can then "Build" the project and "Run" your
toy program.

Data Structures and Algorithms (CS-245) 2


UNIVERSITY OF POONCH RAWALAKOT

   CodeBlocks' Common Errors:


Cannot Compile any C/C++ Program after Installing CodeBlocks:
Check:
1. You downloaded the CodeBlocks with "MinGW GNU C/C++ Compiler" (e.g.,
"codeblocks-20.03-setup.exe").
2. Goto "Settings" menu ⇒ "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, e.g., "c:\Program Files\codeblocks\MinGW" suppose
that CodeBlocks is installed in "c:\Program Files\codeblocks".
Cannot Build or Run Program - Build/Run Buttons and Menu-Items are Grey and
Not Selectable
A previous program is still running. You need to terminate the program by closing the output
console window.
Error: undefined reference to `WinMain@16'
Check that you have a main() function in your function. Check your spelling of main!
(For C Programs) System Error: "xxx.exe has stopped working"
Check your scanf() function. You probably omitted the '&' before the variable name.

Task 01: Write the code in C++ and show the output Hello World :

Output:__________________________________________________________________

Data Structures and Algorithms (CS-245) 3


UNIVERSITY OF POONCH RAWALAKOT

Conclusions:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
_________________________________________________

Remarks:

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
__________________________________________________

_____________
Signature

Data Structures and Algorithms (CS-245) 4

You might also like