1 inf160 is development environments aubg, cos dept lecture 06 title: dev env: code::blocks (extract...

44
1 INF160 IS Development Environments AUBG, COS dept Lecture 06 Title: Dev Env: Code::Blocks (Extract from Syllabus) Reference: www.codeblocks.org

Upload: godfrey-kelly

Post on 27-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

1

INF160 IS Development Environments AUBG, COS dept

Lecture 06Title:

Dev Env: Code::Blocks(Extract from Syllabus)

Reference: www.codeblocks.org

2

Lecture Contents:

Code::Blocks – introductionCode::Blocks – functionalityCode::Blocks – configuration

3

Code::Blocks – introduction

Code::Blocks: The open source, cross platform, free C, C++ and Fortran IDE.

Code::Blocks is a free C, C++ and Fortran IDE built to meet the most demanding needs of its users.

Built around a plugin framework, Code::Blocks can be extended with plugins. Any kind of functionality can be added by installing/coding a plugin. For instance, compiling and debugging functionality is already provided by plugins!

Code::Blocks 13.12 is here! (as of March 2014)

4

Code::Blocks functionality

Code::Blocks includes:

Code Editor

Compiler

Debugger

Other SW tools

5

Code::Blocks

How to Use?

6

Code::Blockshow to use

To download and set up Dev-C++: Already installed in COS lab120

7

Code::Blocks how to use

To start Code::Blocks:

Start Menu >All Programs >CodeBlocks >CodeBlocks

Read and Close the “Tip of the Day” dialog box

8

Code::Blocks how to use

To create project .cbp file File > New > Project… Select project category/type Name the project – select project title Locate the project – select folder to locate project

in

9

Code::Blocks how to use

To add a new source file to an open project, in case your choice was Empty project File > New > Empty File Type the source text of C++ program. Save the file

• File > Save As

10

Code::Blocks how to use

To add an existing file to an open project: Project > Add Files… Locate and then click the name of the file you

want to add to the project, and then click the Open button

11

Code::Blocks how to use

To run a C++ program: Type your source text Save your project: How? (File > Save All) Compile the source: How? (Build > Build) Read, Analyze Compiler Progress messages Run the program: How? (Build > Run)

12

Code::Blocks how to use

To debug a C++ program: If there are compiler errors, you should re-edit

your source text in order to get successful compilation

13

Code::Blocks how to use

To print program’s instructions: File > Print… Dialog box opens (check box line numbers?)

14

Code::Blocks how to use

To close a project: File > Close Projector File > Close all Projectsor File > Close workspace

15

Code::Blocks how to use

To open an existing project, .cbp file: File > Open… Open File dialog box gets opened Navigate to the folder that contains the

project .cbp file Click Open button

16

Code::Blocks

How to Configure?

17

Code::Blocks how to configure

Note: The asterisk on the MyFirstSourceFile.cpp tab indicates that the changes made to the file have not been saved.

18

Code::Blocks how to configure

NOTE: The font type and size used by default are Courier New and 14 point, respectively. If you want to change your editor window’s font type or size, click

Settings > Editor… > Editor settings tab, click Choose

When you are finished, click the OK button.

19

Code::Blocks how to configure

NOTE: You can change the appearance (for example, the font, size, colors, and so on) of the Command Prompt window by right-clicking the window’s title bar and then clicking Properties.

When you are finished, click the Ok button to close the Properties window.

20

Code::Blocks how to configure

NOTE:To display lines numbers in the editor window:

1. Settings > Editor… > Editor settings tab, 2. Section other options, select the Line

Numbers check box.

21

Code::Blocks how to configure

Settings > Environment…

Settings > Editor…

Settings > Compiler…

Settings > Debugger…

22

Exercises/TasksCode::BlocksWrite a C++ program to:display your personal data incl name, age,

email addressRead two numeric data itemsCompute and display result of their addition,

subtraction, multiplication, division and modulus (in case of integer data).

23

Exercises/Tasks To demonstrate advantages of Code::Blocks Back to MS Visual Studio Write a C++ program to operate on arrays: #include <iostream> using namespace std; void main() { int m; // int m=10; cin>>m; const int m=10; int arr[m]; for(int i=0; i<m; i++) { arr[i]=i*10; cout << arr[i] << ‘

‘; } }

24

Exercises/Tasks To demonstrate advantages of Code::Blocks Back to Code::Blocks Write a C++ program to operate on arrays: #include <iostream> using namespace std; void main() { int m; // int m=10; cin>>m; const int m=10; int arr[m]; for(int i=0; i<m; i++) { arr[i]=i*10; cout << arr[i] << ‘ ‘; } }

25

Exercises/Tasks To demonstrate command line arguments in Code::Blocks Back to Code::Blocks Write a C++ program: void main(int argc, char *argv[]) { for(int i=0; i<argc; i++) { cout << ‘\n’ << argv[i]; } } Run the program without command-line arguments Run the program with command-line arguments. How to

specify? Open cmd line window and run the program

26

Exercises/TasksWrite CompactProg project to calculate

Factorilal: facti(7), factr(7) GCD: gcdi(40,15), gcdr(40,15) Fibonacci: fibi(5), fibr(5)

All functions and main() in the same file

27

Exercises/Tasks#include <cstdlib>#include <iostream>

using namespace std;

int factr(int); int facti(int); int gcdr(int, int); int fibr(int);

int main(int argc, char *argv[]){ cout << factr(6) << “ “ << facti(6)<< “ “;

cout << ‘\n’ << gcdr(40,15);cout ,, ‘\n’ << fibr(5);

system("PAUSE"); return EXIT_SUCCESS;}

28

Exercises/Tasksint factr(int n){ if (n==0 ) return 1; return n *factr(n-1); }

int facti(int n){ if (n==0 ) return 1; int res=1; for (int i=1; i <=n; i++) res *=i; return res; }

29

Exercises/Tasksint gcdr(int m, int n){ if (n==0 ) return m; return gcdr(n, m%n); }

int fibr(int n){ if (n==0 || n==1 ) return 1; return fibr(n-1) + fibr(n-2); }

30

Exercises/TasksTo demonstrate:

How to create a static library How to use a static library

31

Exercises/TasksWrite SBCreateStaticLib project to create a static

library .a fileAll functions definitions in one or more filesFile > New > Project… Select Static Library >GoType project titleType folder to create project in Select Debug / Release optionBuild > Build Static library .a file gets created

32

File functions01.cpp//// factorial - recursion, iteration//int factr(int n){ if (n==0 ) return 1; return n *factr(n-1); }int facti(int n){ if (n==0 ) return 1; int res=1; for (int i=1; i <=n; i++) res *=i; return res; }//// greatest common divisor - recursion, iteration//int gcdr(int m, int n){ if (n==0 ) return m; return gcdr(n, m%n); }

int fibr(int n){ if (n==0 || n==1 ) return 1; return fibr(n-1) + fibr(n-2); }

33

Exercises/TasksWrite SBUseStaticLib project to use static library .a fileSource file – only main() functionFile > New > Project… Select Console Application >GoType project titleType folder to create project in Select Debug / Release optionAttention! It’s must to add the user library file. How?

34

Exercises/TasksHow?

1. To set the location of the user library file

2. to set the library name

35

Exercises/Tasks1. To set the location of the user library fileSettings >Compiler… > Search Directories

tabselect Linker TabAdd the folder where the library file is

in

36

Exercises/Tasks2. to set the library nameRight click on the bolded project name Choose Build optionsClick the linker tab. Under the “Link libraries”

window, press the “Add” button and add the library you wish your project to use.

Press OK button

37

Exercises/TasksTo run the application

Build > Build Build > Run

Or

Build > Build and Run

38

File mainUseStaticLib01.cpp

#include <cstdlib>#include <iostream>

using namespace std;

int facti(int), factr(int), fibr(int), gcdr(int, int); int main(int argc, char *argv[]){ cout << "\n Factoial = " << factr(6) << " " << facti(6);

cout << "\n GCDivisor = " << gcdr(40,15); cout << "\n Fibonacci = " << fibr(4);

system("PAUSE"); return EXIT_SUCCESS;}

39

Exercises/TasksTo demonstrate:

How to create a second static library How to use a second static library

40

File functions02.cpp

#include <iostream>

void MyName(){ std::cout << "\n\n My name is Stoyan\n"; }void MyAddress(){ std::cout << "\n\n My address is 1504 Sofia, BG\

n"; }

41

File mainUseStaticLib02.cpp#include <cstdlib>#include <iostream>

using namespace std;

void MyName(), MyAddress();

int main(int argc, char *argv[]){ MyName(); MyAddress(); system("PAUSE"); return EXIT_SUCCESS;}

42

Exercises/TasksTo demonstrate:

How to use two static libraries in the same project

43

File mainUseTwoLibraries.cpp#include <cstdlib>#include <iostream>

using namespace std;

int facti(int), factr(int), fibr(int), gcdr(int, int);void MyName(), MyAddress();

int main(int argc, char *argv[]){ cout << "\n Factoial = " << factr(6) << " " << facti(6);

cout << "\n GCDivisor = " << gcdr(40,15); cout << "\n Fibonacci = " << fibr(4);

MyName(); MyAddress(); system("PAUSE"); return EXIT_SUCCESS;}

44

Thank You For

Your Attention!