chapter ten developing unix applications in c and c++
DESCRIPTION
3 Objectives Create simple C programs Understand C program structure Use the make utility to help with program development and maintenanceTRANSCRIPT
![Page 1: Chapter Ten Developing UNIX Applications In C and C++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/1.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/2.jpg)
22
Lesson ALesson A
C Programming in aC Programming in aUNIX EnvironmentUNIX Environment
![Page 3: Chapter Ten Developing UNIX Applications In C and C++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/3.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/4.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/5.jpg)
55
![Page 6: Chapter Ten Developing UNIX Applications In C and C++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/6.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/7.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/8.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/9.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/10.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/11.jpg)
1111
Multiple source filesMultiple source files
![Page 12: Chapter Ten Developing UNIX Applications In C and C++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/12.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/13.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/14.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/15.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/16.jpg)
1616
Lesson BLesson B
C++ Programming in aC++ Programming in aUNIX EnvironmentUNIX Environment
![Page 17: Chapter Ten Developing UNIX Applications In C and C++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/17.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/18.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/19.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/20.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/21.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/22.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/23.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/24.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/25.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/26.jpg)
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++](https://reader030.vdocuments.mx/reader030/viewer/2022012900/5a4d1b7a7f8b9ab0599b891a/html5/thumbnails/27.jpg)
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