INF160Lec06 IDE CodeBlocks

advertisement
INF160 IS Development Environments
AUBG, COS dept
Lecture 06
Title:
Dev Env: Code::Blocks
(Extract from Syllabus)
Reference: www.codeblocks.org
1
Lecture Contents:
 Code::Blocks – introduction
 Code::Blocks – functionality
 Code::Blocks – configuration
2
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)
3
Code::Blocks functionality
 Code::Blocks includes:

Code Editor

Compiler

Debugger

Other SW tools
4
Code::Blocks
How to Use?
5
Code::Blocks
how to use
 To download and set up Dev-C++:

Already installed in COS lab120
6
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
7
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
8
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
9
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
10
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)
11
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
12
Code::Blocks
how to use
 To print program’s instructions:


File > Print…
Dialog box opens (check box line numbers?)
13
Code::Blocks
how to use
 To close a project:
File > Close Project
or
 File > Close all Projects
or
 File > Close workspace

14
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
15
Code::Blocks
How to Configure?
16
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.
17
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.
18
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.
19
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.
20
Code::Blocks
how to configure
 Settings > Environment…
 Settings > Editor…
 Settings > Compiler…
 Settings > Debugger…
21
Exercises/Tasks
 Code::Blocks
 Write a C++ program to:
 display your personal data incl name, age, email
address
 Read two numeric data items
 Compute and display result of their addition,
subtraction, multiplication, division and modulus
(in case of integer data).
22
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] << ‘ ‘; }
}
23
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] << ‘ ‘; }
}
24
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
25
Exercises/Tasks
 Write 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
26
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;
}
27
Exercises/Tasks
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;
}
28
Exercises/Tasks
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);
}
29
Exercises/Tasks
 To demonstrate:


How to create a static library
How to use a static library
30
Exercises/Tasks
 Write SBCreateStaticLib project to create a static
library .a file
 All functions definitions in one or more files
 File > New > Project… Select Static Library >Go
 Type project title
 Type folder to create project in
 Select Debug / Release option
 Build > Build
Static library .a file gets created
31
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);
}
32
Exercises/Tasks
 Write SBUseStaticLib project to use static library .a
file
 Source file – only main() function
 File > New > Project… Select Console Application >Go
 Type project title
 Type folder to create project in
 Select Debug / Release option
 Attention! It’s must to add the user library file. How?
33
Exercises/Tasks
 How?
 1. To set the location of the user library file
 2. to set the library name
34
Exercises/Tasks
 1. To set the location of the user library file
Settings >Compiler… > Search Directories tab
select Linker Tab
Add the folder where the library file is in
35
Exercises/Tasks
 2. to set the library name
Right click on the bolded project name
Choose Build options
Click 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
36
Exercises/Tasks
 To run the application

Build > Build
Build > Run

Or

Build > Build and Run

37
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;
38
Exercises/Tasks
 To demonstrate:


How to create a second static library
How to use a second static library
39
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";
}
40
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;
}
41
Exercises/Tasks
 To demonstrate:

How to use two static libraries in the same project
42
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) << "
cout << "\n GCDivisor = " << gcdr(40,15);
cout << "\n Fibonacci = " << fibr(4);
}
MyName();
MyAddress();
system("PAUSE");
return EXIT_SUCCESS;
" << facti(6);
43
Thank You
For
Your Attention!
44
Download