27
Chapter Ten Chapter Ten Developing UNIX Developing UNIX Applications Applications In C and C++ In C and C++

Chapter Ten Developing UNIX Applications In C and C++

Embed Size (px)

DESCRIPTION

3 Objectives Create simple C programs Understand C program structure Use the make utility to help with program development and maintenance

Citation preview

Page 1: Chapter Ten Developing UNIX Applications In C and C++

Chapter TenChapter Ten

Developing UNIX Developing UNIX ApplicationsApplicationsIn C and C++In C and C++

Page 2: Chapter Ten Developing UNIX Applications In C and C++

22

Lesson ALesson A

C Programming in aC Programming in aUNIX EnvironmentUNIX Environment

Page 3: Chapter Ten Developing UNIX Applications In C and C++

33

ObjectivesObjectivesCreate 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

Page 4: Chapter Ten Developing UNIX Applications In C and C++

44

Introducing C ProgrammingIntroducing C ProgrammingC 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

Page 5: Chapter Ten Developing UNIX Applications In C and C++

55

Page 6: Chapter Ten Developing UNIX Applications In C and C++

66

Creating a C ProgramCreating a C Program#include <stdio.h>

main() { printf("hello world\n");}

Stored as:Stored as: one.cone.cCompiled and linked as:Compiled and linked as: gcc one.cgcc one.crun as:run as: ./a.out./a.out

Page 7: Chapter Ten Developing UNIX Applications In C and C++

77

Compilation ProcessCompilation Process.c and .h

.o

a.out

.i

.s

gcc -E

gcc -S

gcc -c

.a ar

gcc -o

Page 8: Chapter Ten Developing UNIX Applications In C and C++

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

Page 9: Chapter Ten Developing UNIX Applications In C and C++

99

A simple C programA simple C program

to compile:to compile:gcc two.cgcc two.c

produces:produces: a.out a.outororgcc -c two.cgcc -c two.c

produces:produces: two.o two.o

Page 10: Chapter Ten Developing UNIX Applications In C and C++

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

Page 11: Chapter Ten Developing UNIX Applications In C and C++

1111

Multiple source filesMultiple source files

Page 12: Chapter Ten Developing UNIX Applications In C and C++

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

Page 13: Chapter Ten Developing UNIX Applications In C and C++

1313

Steps to create an executableSteps to create an executable

to compile:to compile:gcc –c compute.cgcc –c compute.cgcc –c power.cgcc –c power.cgcc compute.o power.o –lm –o powergcc compute.o power.o –lm –o powerto runto run./power./power

Page 14: Chapter Ten Developing UNIX Applications In C and C++

1414

make utilitymake utility

has configuration file that specifies has configuration file that specifies dependencies: dependencies: makefilemakefilechecks timestamps on fileschecks timestamps on filesrecompiles necessary filesrecompiles necessary files

Page 15: Chapter Ten Developing UNIX Applications In C and C++

1515

makefilemakefilepower: power.o compute.opower: power.o compute.o gcc power.o compute.o -o power -lmgcc power.o compute.o -o power -lmpower.o: power.cpower.o: power.c gcc -c power.cgcc -c power.ccompute.o: compute.ccompute.o: compute.c gcc -c compute.cgcc -c compute.c

Page 16: Chapter Ten Developing UNIX Applications In C and C++

1616

Lesson BLesson B

C++ Programming in aC++ Programming in aUNIX EnvironmentUNIX Environment

Page 17: Chapter Ten Developing UNIX Applications In C and C++

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

Page 18: Chapter Ten Developing UNIX Applications In C and C++

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

Page 19: Chapter Ten Developing UNIX Applications In C and C++

1919

Creating a C++ ProgramCreating a C++ Program#include <iostream>

main() { cout << "hello world" << endl;}

Stored as:Stored as: one.ccone.ccCompiled and linked as:Compiled and linked as: g++ one.ccg++ one.ccrun as:run as: ./a.out./a.out

Page 20: Chapter Ten Developing UNIX Applications In C and C++

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

Page 21: Chapter Ten Developing UNIX Applications In C and C++

2121

A simple C++ programA simple C++ program

to compile:to compile:g++ two.ccg++ two.cc

produces:produces: a.out a.outororg++ -c two.ccg++ -c two.cc

produces:produces: two.o two.o

Page 22: Chapter Ten Developing UNIX Applications In C and C++

2222

C++ classesC++ classes

Class header and bodyClass header and bodyHeader in header fileHeader in header file .h.hBody in body fileBody in body file .cc or .C.cc or .C– includes header fileincludes header file

main function still requiredmain function still required

Page 23: Chapter Ten Developing UNIX Applications In C and C++

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

Page 24: Chapter Ten Developing UNIX Applications In C and C++

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;}}

Page 25: Chapter Ten Developing UNIX Applications In C and C++

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;}}

Page 26: Chapter Ten Developing UNIX Applications In C and C++

2626

makefilemakefilecount: Counter.o main.ocount: Counter.o main.o g++ Counter.o main.o -o countg++ Counter.o main.o -o countCounter.o: Counter.CCounter.o: Counter.C g++ -c Counter.Cg++ -c Counter.Cmain.o: main.Cmain.o: main.C g++ -c main.Cg++ -c main.C

Page 27: Chapter Ten Developing UNIX Applications In C and 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