Upload
beatrix-chase
View
232
Download
0
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
Chapter TenChapter Ten
Developing UNIX Developing UNIX ApplicationsApplicationsIn C and C++In C and C++
22
Lesson ALesson A
C Programming in aC Programming in aUNIX EnvironmentUNIX Environment
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
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
55
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
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.outororgcc -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.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
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
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
1616
Lesson BLesson B
C++ Programming in aC++ Programming in aUNIX 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.ccCompiled and linked as:Compiled and linked as: g++ one.ccg++ one.ccrun 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.outororg++ -c two.ccg++ -c two.cc
produces:produces: two.o two.o
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
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
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
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