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

Chapter Ten

Jan 01, 2016

Download

Documents

Megan Douglas

Chapter Ten. Developing UNIX Applications In C and C++. Lesson A. C Programming in a UNIX Environment. Objectives. Create simple C programs Understand C program structure Use the make utility to help with program development and maintenance. Introducing C Programming. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter Ten

Chapter TenChapter Ten

Developing UNIX Developing UNIX ApplicationsApplications

In C and C++In C and C++

Page 2: Chapter Ten

22

Lesson ALesson A

C Programming in aC Programming in a

UNIX EnvironmentUNIX Environment

Page 3: Chapter Ten

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

Page 4: Chapter Ten

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

Page 5: Chapter Ten

55

Page 6: Chapter Ten

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

Page 7: Chapter Ten

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

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

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

Page 10: Chapter Ten

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

1111

Multiple source filesMultiple source files

Page 12: Chapter Ten

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

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

Page 14: Chapter Ten

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

Page 15: Chapter Ten

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

Page 16: Chapter Ten

1616

Lesson BLesson B

C++ Programming in aC++ Programming in a

UNIX EnvironmentUNIX Environment

Page 17: Chapter Ten

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

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

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

Page 20: Chapter Ten

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

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

Page 22: Chapter Ten

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

Page 23: Chapter Ten

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

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

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

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

Page 27: Chapter Ten

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


Related Documents