1 / 27

Chapter Ten

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.

Download Presentation

Chapter Ten

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Lesson A C Programming in a UNIX Environment

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

  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. Creating a C Program #include <stdio.h> main() { printf("hello world\n"); } • Stored as: one.c • Compiled and linked as: gcc one.c • run as: ./a.out

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

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

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

  9. A simple C function #include <math.h> int compute(int x, int y) { double tx = x, ty = y, tr; tr = pow(tx, ty); return (int) tr; } • uses math library

  10. Multiple source files

  11. A main program #include <stdio.h> main() { int x, y, p; printf("This program computes x^y\n"); printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); p = compute(x, y); printf("Result: %d\n", p); } • asks for user input

  12. 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

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

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

  15. Lesson B C++ Programming in a UNIX Environment

  16. Objectives • Create a simple C++ • understand multi-file organization of a C++ program • make

  17. 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

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

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

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

  21. 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

  22. A simple C++ class header #ifndef COUNTER_H #define COUNTER_H class Counter { int value; public: Counter(); void increment(int); void reset(); int getValue(); }; #endif

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

  24. main C++ program #include <iostream> #include "Counter.h" main() { Counter c; int value; cout << "enter value: "; cin >> value; c.increment(value); cout << "counter now: " << c.getValue() << endl; }

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

  26. 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

More Related