Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Ten Developing UNIX Applications In C and C++

Similar presentations


Presentation on theme: "Chapter Ten Developing UNIX Applications In C and C++"— Presentation transcript:

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

2 2 Lesson A C Programming in a UNIX Environment

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

4 4 Introducing C Programming C is the language in which UNIX was developed and refined Known for its efficiency and brevity C program is set of functions –Must have main function C programs are stored in –Header files:.h –Body files:.c

5 5

6 6 Creating a C Program #include main() { printf("hello world\n"); } Stored as: one.c Compiled and linked as: gcc one.c run as:./a.out

7 7 Compilation Process.c and.h.o a.out.i.s gcc -E gcc -S gcc -c.a ar gcc -o

8 8 A simple C program #include #include main(int argc, char* argv[]) { if (argc > 1) if (argc > 1) printf("hello %s\n", argv[1]); printf("hello %s\n", argv[1]); else else printf("hello world\n"); printf("hello world\n");} command line arguments

9 9 A simple C program to compile: gcc two.c produces: a.out or gcc -c two.c produces: two.o

10 10 A simple C function #include #include 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 library

11 11 Multiple source files

12 12 A main program #include #include 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 input

13 13 Steps to create an executable to compile: gcc –c compute.c gcc –c power.c gcc compute.o power.o –lm –o power to run./power

14 14 make utility has configuration file that specifies dependencies: makefile checks timestamps on files recompiles necessary files

15 15 makefile power: power.o compute.o gcc power.o compute.o -o power -lm gcc power.o compute.o -o power -lm power.o: power.c gcc -c power.c gcc -c power.c compute.o: compute.c gcc -c compute.c gcc -c compute.c

16 16 Lesson B C++ Programming in a UNIX Environment

17 17 Objectives Create a simple C++ understand multi-file organization of a C++ program make

18 18 Introducing C++ Programming C++ is a programming language that builds on C to add object-oriented capabilities C and C++ are similar in many ways

19 19 Creating a C++ Program #include main() { cout << "hello world" << endl; } Stored as: one.cc Compiled and linked as: g++ one.cc run as:./a.out

20 20 A simple C++ program #include #include main(int argc, char* argv[]) { if (argc > 1) if (argc > 1) cout << "hello " << argv[1] << endl; cout << "hello " << argv[1] << endl; else else cout << "hello world\n"; cout << "hello world\n";} command line arguments

21 21 A simple C++ program to compile: g++ two.cc produces: a.out or g++ -c two.cc produces: two.o

22 22 C++ classes Class header and body Header in header file.h Body in body file.cc or.C –includes header file main function still required

23 23 A simple C++ class header #ifndef COUNTER_H #define COUNTER_H 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

24 24 A simple C++ class body #include "Counter.h" Counter::Counter() { reset(); reset();} void Counter::increment(int n=1) { value += n; value += n;} void Counter::reset() { value = 0; value = 0;} int Counter::getValue() { return value; return value;}

25 25 main C++ program #include #include #include "Counter.h" 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;}

26 26 makefile count: Counter.o main.o g++ Counter.o main.o -o count g++ Counter.o main.o -o count Counter.o: Counter.C g++ -c Counter.C g++ -c Counter.C main.o: main.C g++ -c main.C g++ -c main.C

27 27 Chapter Summary The major difference between C and C++ is that C follows procedural principles and C++ primarily follows object-oriented programming The make utility is used to maintain the application’s source files


Download ppt "Chapter Ten Developing UNIX Applications In C and C++"

Similar presentations


Ads by Google