Chapter Ten
description
Transcript of Chapter Ten
Chapter TenChapter Ten
Developing UNIX Developing UNIX ApplicationsApplications
In C and C++In C and C++
22
Lesson ALesson A
C Programming in aC Programming in a
UNIX EnvironmentUNIX Environment
33
ObjectivesObjectives
Create simple C programsCreate simple C programs
Understand C program structureUnderstand C program structure
Use the make utility to help with program Use the make utility to help with program development and maintenancedevelopment and maintenance
44
Introducing C ProgrammingIntroducing C Programming
C is the language in which UNIX was C is the language in which UNIX was developed and refineddeveloped and refined
Known for its efficiency and brevityKnown for its efficiency and brevity
C program is set of functionsC program is set of functions– Must have Must have mainmain function function
C programs are stored inC programs are stored in– Header files: Header files: .h.h– Body files:Body files: .c.c
55
66
Creating a C ProgramCreating a C Program#include <stdio.h>
main() { printf("hello world\n");}
Stored as:Stored as: one.cone.c
Compiled and linked as:Compiled and linked as: gcc one.cgcc one.c
run as:run as: ./a.out./a.out
77
Compilation ProcessCompilation Process.c and .h
.o
a.out
.i
.s
gcc -E
gcc -S
gcc -c
.a ar
gcc -o
88
A simple C programA simple C program
#include <stdio.h>#include <stdio.h>main(int argc, char* argv[]) {main(int argc, char* argv[]) { if (argc > 1)if (argc > 1) printf("hello %s\n", argv[1]);printf("hello %s\n", argv[1]); elseelse printf("hello world\n");printf("hello world\n");}}
command line argumentscommand line arguments
99
A simple C programA simple C program
to compile:to compile:
gcc two.cgcc two.c
produces:produces: a.out a.out
oror
gcc -c two.cgcc -c two.c
produces:produces: two.o two.o
1010
A simple C functionA simple C function
#include <math.h>#include <math.h>
int compute(int x, int y) {int compute(int x, int y) { double tx = x, ty = y, tr;double tx = x, ty = y, tr; tr = pow(tx, ty);tr = pow(tx, ty); return (int) tr;return (int) tr;}}
uses math libraryuses math library
1111
Multiple source filesMultiple source files
1212
A main programA main program#include <stdio.h>#include <stdio.h>main() {main() { int x, y, p;int x, y, p; printf("This program computes x^y\n");printf("This program computes x^y\n"); printf("Enter x: ");printf("Enter x: "); scanf("%d", &x);scanf("%d", &x); printf("Enter y: ");printf("Enter y: "); scanf("%d", &y);scanf("%d", &y); p = compute(x, y);p = compute(x, y); printf("Result: %d\n", p);printf("Result: %d\n", p);}}
asks for user inputasks for user input
1313
Steps to create an executableSteps to create an executable
to compile:to compile:
gcc –c compute.cgcc –c compute.c
gcc –c power.cgcc –c power.c
gcc compute.o power.o –lm –o powergcc compute.o power.o –lm –o power
to runto run
./power./power
1414
make utilitymake utility
has configuration file that specifies has configuration file that specifies dependencies: dependencies: makefilemakefile
checks timestamps on fileschecks timestamps on files
recompiles necessary filesrecompiles necessary files
1515
makefilemakefile
power: power.o compute.opower: power.o compute.o
gcc power.o compute.o -o power -lmgcc power.o compute.o -o power -lm
power.o: power.cpower.o: power.c
gcc -c power.cgcc -c power.c
compute.o: compute.ccompute.o: compute.c
gcc -c compute.cgcc -c compute.c
1616
Lesson BLesson B
C++ Programming in aC++ Programming in a
UNIX EnvironmentUNIX Environment
1717
ObjectivesObjectives
Create a simple C++Create a simple C++
understand multi-file organization of a understand multi-file organization of a C++ programC++ program
makemake
1818
Introducing C++ Introducing C++ ProgrammingProgramming
C++ is a programming language that C++ is a programming language that builds on C to add object-oriented builds on C to add object-oriented capabilitiescapabilities
C and C++ are similar in many waysC and C++ are similar in many ways
1919
Creating a C++ ProgramCreating a C++ Program#include <iostream>
main() { cout << "hello world" << endl;}
Stored as:Stored as: one.ccone.cc
Compiled and linked as:Compiled and linked as: g++ one.ccg++ one.cc
run as:run as: ./a.out./a.out
2020
A simple C++ programA simple C++ program
#include <iostream>#include <iostream>
main(int argc, char* argv[]) {main(int argc, char* argv[]) { if (argc > 1)if (argc > 1) cout << "hello " << argv[1] << endl;cout << "hello " << argv[1] << endl; elseelse cout << "hello world\n";cout << "hello world\n";}}
command line argumentscommand line arguments
2121
A simple C++ programA simple C++ program
to compile:to compile:
g++ two.ccg++ two.cc
produces:produces: a.out a.out
oror
g++ -c two.ccg++ -c two.cc
produces:produces: two.o two.o
2222
C++ classesC++ classes
Class header and bodyClass header and body
Header in header fileHeader in header file .h.h
Body in body fileBody in body file .cc or .C.cc or .C– includes header fileincludes header file
main function still requiredmain function still required
2323
A simple C++ class headerA simple C++ class header
#ifndef COUNTER_H#ifndef COUNTER_H#define COUNTER_H#define COUNTER_Hclass Counter {class Counter { int value;int value; public:public: Counter();Counter(); void increment(int);void increment(int); void reset();void reset(); int getValue();int getValue();};};#endif#endif
2424
A simple C++ class bodyA simple C++ class body
#include "Counter.h"#include "Counter.h"
Counter::Counter() {Counter::Counter() { reset();reset();}}void Counter::increment(int n=1) {void Counter::increment(int n=1) { value += n;value += n;}}void Counter::reset() {void Counter::reset() { value = 0;value = 0;}}int Counter::getValue() {int Counter::getValue() { return value;return value;}}
2525
main C++ programmain C++ program
#include <iostream>#include <iostream>#include "Counter.h"#include "Counter.h"main() {main() { Counter c;Counter c; int value;int value; cout << "enter value: ";cout << "enter value: "; cin >> value;cin >> value; c.increment(value);c.increment(value); cout << "counter now: " cout << "counter now: " << c.getValue() << endl;<< c.getValue() << endl;}}
2626
makefilemakefile
count: Counter.o main.ocount: Counter.o main.o
g++ Counter.o main.o -o countg++ Counter.o main.o -o count
Counter.o: Counter.CCounter.o: Counter.C
g++ -c Counter.Cg++ -c Counter.C
main.o: main.Cmain.o: main.C
g++ -c main.Cg++ -c main.C
2727
Chapter SummaryChapter Summary
The major difference between C and C++ The major difference between C and C++ is that C follows procedural principles is that C follows procedural principles and C++ primarily follows object-oriented and C++ primarily follows object-oriented programmingprogramming
The make utility is used to maintain the The make utility is used to maintain the application’s source filesapplication’s source files