C Programming Tools Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: 818-677-4954 “UNIX for Programmers and Users” Third Edition,

Slides:



Advertisements
Similar presentations
Linking & Loading CS-502 Operating Systems
Advertisements

Gdb: GNU Debugger Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
COSC 120 Computer Programming
Ar: Unix Archiver Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Introduction to Shell Script Programming
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction Unix-like system is everywhere Linux Android for smartphones Google Chrome OS for Chromebook Web.
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
Lecture 5 Software Development. Types of Development Tools Archiving: tar, cpio, pax, RPM Configuration: autoconf Compilation and building: make Managing.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Programming With C.
CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Programmable Text Processing with awk Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users”
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
SEEM3460 Tutorial Multi-module programming in C. Copy the material Create the directory mkdir c_multi cd c_multi mkdir ask cd ask Copy files cp ~seem3460/distribute/c_multi-module/ask/ask_reverse.c.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Chapter 12 C Programming Tools Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original Notes.
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
Introduction to Programming Using C An Introduction to Operating Systems.
CVS: Concurrent Version System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Make: File Dependency System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
cs33201 Chapter 12 C Programming Tools Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003.
C P ROGRAMMING T OOLS. C OMPILING AND R UNNING S INGLE M ODULE P ROGRAM.
 Simple UNIX commands  I/O techniques in C++  Solutions to Lab#0 problems  Solutions to Lab#1 problems 1.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
 Prepared by: Eng. Maryam Adel Abdel-Hady
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
C 프로그래밍 개발 도구. 컴파일러 gnu c compiler 컴파일 gcc sample.c 실행 a.out 실행 파일 지정 컴파일 gcc –o sample sample.c 실행 sample.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Review Why do we use protection levels? Why do we use constructors?
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Ar: Unix Archiver Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Lecture 3 Translation.
Introduction to C Topics Compilation Using the gcc Compiler
SEEM3460 Tutorial Unix Introduction.
Multi-module programming in C
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Objectives Identify the built-in data types in C++
Linking & Loading.
Introduction to C Topics Compilation Using the gcc Compiler
CS-3013 Operating Systems C-term 2008
Introduction to C Topics Compilation Using the gcc Compiler
Loaders and Linkers: Features
SEEM3460 Tutorial Unix Introduction Xinshi Lin & Zihao Fu
Multi-module programming in C
Multi-module programming in C
Linking & Loading CS-502 Operating Systems
CSE 303 Concepts and Tools for Software Development
Appendix F C Programming Environment on UNIX Systems
Linking & Loading CS-502 Operating Systems
Compiler vs linker The compiler translates one .c file into a .o file
Introduction to C Topics Compilation Using the gcc Compiler
Review.
SPL – PS1 Introduction to C++.
Presentation transcript:

C Programming Tools Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: Tools that support the various stages of program development: compilation, debugging, maintaining libraries, profiling, and source code control.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  The C Languages - K&R C - ANSI C

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  SINGLE-MODULE PROGRAMS We will analyze a simple C program: - performs a simple task: reversing a string. - how to write, compile, link, and execute a program that solves the problem using a single source file.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: source-code listing of the first version of the “reverse” program (“reversefirst.c” on the Web) /* REVERSE.C */ #include /* Function Prototype */ void reverse(); /*****************************************/ main() { char str[100]; /* Buffer to hold reversed string */ reverse(“cat”, str); /* Reverse the string “cat” */ printf(“reverse (“cat”)=%s\n”, str); /* Display Result */ reverse(“noon”, str); /* Reverse the string “noon” */ printf(“reverse (“noon”) = %s\n”, str); /* Display Result */ } /****************************************/

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: void reverse(before, after) char *before; /* A pointer to the source string */ char *after; /* A pointer to the reversed string */ { int i; int j; int len; len = strlen( before ); for( j=len-1; i=0; j>=0; j--; i++) /* Reverse loop */ after[i] = before[j]; after[len] = NULL; /* NULL terminate reversed string */ }

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Compiling a C Program - To create and run the “reverse” program, 1) create a subdirectory called “reverse” inside your home directory. 2) create the file “reversefirst.c” using the vi editor 3) compile the C program using the cc utility. - By default, cc creates an executable file called “a.out” in the current directory. To run the program, type “./a.out”.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Compiling a C Program ( Continue ) - Any errors that are encountered are sent to the standard error channel, which is connected by default to your terminal’s screen. $ mkdir reverse $ cd reverse $ vi reversefirst.c … $ cc reversefirst.c “reversefirst.c”, line 16: syntax error at or near variable name “cat” “reversefirst.c”, line 18: syntax error at or near variable name “noon” “reversefirst.c”, line 35: syntax error at or near symbol ; “reversefirst.c”, line 35: syntax error at or near symbol ) $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Compiling a C Program ( Continue ) - cc found a number of compile-time errors:  The errors on line 16 and 18 due to inappropriate use of double quotes within double quotes.  The errors one line 35 were due to an illegal use of a semicolon(;). -

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  A Listing of the Corrected “Reverse” Program /* REVERSE.C */ #include /* Function Prototype */ void reverse(); /******************************************/ main() { char str[100]; /* Buffer to hold reversed string */ reverse(“cat”, str); /* Reverse the string “cat” */ printf(“reverse (\” cat \” ) = %s \n”, str); /* Display */ reverse(“noon”, str); /* Reverse the string “noon” */ printf(“reverse (\” noon \” ) = %s \n”, str); /* Display */ } /*****************************************/

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: void reverse(before, after) char *before; /* A pointer to the source string */ char *after; /* A pointer to the reversed string */ { int i; int j; int len; len = strlen(before); for( j=len -1, i=0; j>=0; j--,j++) /* Reverse loop */ after[i] = before[j]; after[len]=NULL; /* NULL terminate reversed string */ )

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Running a C Program - After compiling the second version of “reverse.c”, by typing the name of the executable file, “./a.out”. $ cc reverse.c $ ls -l reverse.c a.out -rwxr-xr-x 1 glass Jan5 16:16 a.out* -rw-r--r-- 1 glass 439 Jan5 16:15 reverse.c $./a.out reverse (“cat”) = tac reverse (“noon”) = noon $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Overriding the Default Executable Name - to use the “-o” option with cc, which allows you to specify the name of the executable file that you wish to create: $ cc reverse.c -o reverse $ ls -l reverse -rwx-xr-x 1 glass Jan 5 16:19 reverse* $./reverse reverse(“cat”) = tac reverse(“noon”) = noon $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  MULTIMODULE PROGRAMS - The trouble with the “reverse” program, the “reverse” function cannot easily be used in other programs. - to write a function that returns a value of 1 if a string is a palindrome and a value of 0 otherwise. - A palindrome a string that reads the same forward and backward; for example, “noon” is a palindrome, but “nono” is not. - use the “reverse” function to implement our “palindrome” function.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Reusable Functions - A better strategy for sharing “reverse()”, 1) compile it separately, 2) and then link the resultant object module into whichever programs wish to use it. - this technique allows the function to be used in many different programs. Functions with this property are termed reusable functions.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Preparing a Reusable Function - To prepare a reusable function, 1) create a source-code module that contains the source code of the function, together with a header file that contains the function’s prototype. 2) compile the source-code module into an object module by using the “-c” option of cc.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Preparing a Reusable Function - reverse.h /* REVERSE.H */ void reverse(); /* Declare but do not define this function */ - reverse.c /* REVERSE.C */ #include #include “reverse.h” /********************************************/ void reverse( before, after ) char *before; /* A pointer to the original string */ char *after; /* A pointer to the reversed string */ { int i; int j; int len;

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: len = strlen(before); for( j=len-1, i=0; j>=0; j--, j++) /* Reverse loop */ after[i] = before[j]; after[len] = NULL; /* NULL terminate reversed string */ } - main1.c /* MAIN1.C */ #include #include “reverse.h” /* Contains the protype of reverse() */ /***********************************************/ main() { char str[100]; reverse(“cat”, str); /* Invoke external function */ printf(“reverse (\”cat\”) = %s\n”, str); reverse(“noon”,str); /* Invoke external function */ printf(“reverse (\”noon\”) = %s\n”, str); }

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Separately Compiling and Linking Modules - To compile each source-code file separately, use the “-c” option for cc. creates a separate object module for each source-code file, each with a “.o” suffix. $ cc -c reverse.c $ cc -c main1.c $ ls -l reverse.o mail1.o -rw-r--r-- 1 glass 311 Jan 5 18:24 main1.o -rw-r--r-- 1 glass 181 Jan 5 18:08 reverse.o $ - - place all of the source-code files on one line. $ cc -c reverse.c main1.c $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Separately Compiling and Linking Modules - To link them all together into an executable called “main1”, list the names of all of the object modules after the cc command: $ cc reverse.o main1.o -o main1 $ ls -l main1 -rwxr-xr-x 1 glass Jan 5 18:25 main1* $./main1 reverse (“cat”) = tac reverse (“noon”) = noon $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  The Stand-alone Loader: ld - When cc is used to link several object modules, it transparently invokes the UNIX stand-alone loader, ld, to do the job. - The loader is better known as the linker. ld -n {-Lpath}* {objModule}* {library}* {-lx}* [-o outputFile] ld links together the specified object and library modules to produce an executable file. -n : if you wish to create a stand-alone executable -o : override the default name of the executable, “a.out”, -lx : searches the standard directories “/lib”, “/usr/lib”, and “/usr/local/lib” for a library with the name “libx.a”. -Lpath : to insert the directory path into this search path.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Separately Compiling and Linking Modules - If you link a C program manually, it’s important to specify the C runtime object module, “/lib/crt0.o”, $ ld -n /lib/crt0.o mina1.o reverse.o -lc -o main1 $ main1 reverse (“cat”) = tac reverse (“noon”) = noon $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Reusing the Reverse Function - Here’s the header and source-code listing of the “palindrome” function: palindrome.h /* PALINDROME.H */ int palindrome(); /* Declare but do not define */ palindrome.c /* PALINDROME.C */ #include “palindrome.h” #include “reverse.h” #include /***********************************************/ int palindrome(str) char *str; {

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: char reverseStr[100]; reverse( str, reversedStr ); /* Reverse original */ return ( strcmp(str, reversedStr ) == 0 ); /* Compare the two */ } - the program main2.c that tests “palindrome()” /* MAIN2.C */ #include #include “palindrome.h” /*************************************************/ main() { printf(“palindrome (\”cat\”) = %d \n”, palindrome(“cat”) ); printf(“palindrome (\”noon\”) = %d \n”, palindrome(“noon”) ); }

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: To combine the “reverse”, “palindrome”, and “main2” modules Compile the object modules and then link them. $ cc -c palindrome.c $ cc -c main2.c $ cc reverse.o palindrome.o main2.o -o main2 -rwxr-xr-x 1 glass Jan 5 19:09 main2* -rw-r--r-- 1 glass 306 Jan 5 19:00 main2.o -rw-r--r-- 1 glass 189 Jan 5 18:59 palindrome.o -rw-r--r-- 1 glass 181 Jan 5 18:08 reverse.o $ main2 palindrome (“cat”) = 0 palindrome (“noon”) = 1 $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  Maintaining Multimodule Programs - Several different issues must be considered when maintaining multimodule systems: Q1. What ensures that object modules and executables are kept up to date? Q2. What stores the object modules? Q3. What tracks each version of source and header files? A1. make, the UNIX file-dependency systemmake A2. ar, the UNIX archive systemar A3. cvs, the Concurrent Version Systemcvs

C Programming Tools Debugging Programs with gdb: GNU Debugger gdb: GNU Debugger

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  THE UNIX PROFILER: PROF - To see where a program is spending its time. - If an amount of time that is greater than expected is being spent in a particular function, it might be worth optimizing the function by hand for better performance. Utility : prof -ln [ executableFile [profileFile] ] prof is the standard UNIX profiler. It generates a table of time and repetitions of each function in the executable executableFile based on the performance trace stored in the file profileFile. if profileFile is omitted, the filename “mon.out” is assumed.

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  THE UNIX PROFILER: PROF - The executable file must have been compiled using the -p option of cc, which instructs the compiler to generate special code that writes a “mon.out” file when the program runs. $ cc -p main2.c $ main2---> execute the program. palindrome (“cat”) = 0---> program output. palindrome (“noon”) = 1 $ ls -l mon.out---> list the file for monitor output. -rw-r-xr-x 1 gglass 1472 Jan 8 17:19 mon.out $ prof main2 mon.out---> profile the program. %Time Seconds Cumsecs #Calls msec/call Name rdpcs reverse palindrome main

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  THE UNIX PROFILER: PROF $ prof -l main2---> order the profile by name. %Time Seconds Cumsecs #Calls msec/call Name main palindrome rdpcs reverse $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  DOUBLE-CHECKING PROGRAMS: LINT - checks your program more thoroughly than cc does: Utility : lint { fileName }* lint scans the specified source files and displays any potential errors that it finds. $ lint reverse.c---> check “reverse.c”. reverse defined ( reverse.c(12) ), but never used $ lint palindrome.c---> check “palindrome.c”. palindrome defined ( palindrome.c ( 12 ) ), but never used reverse used ( palindrome.c(14) ), but not defined

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  DOUBLE-CHECKING PROGRAMS: LINT $ lint main2.c---> check “main2.c”. main2.c(11) : warning: main() returns random value to invocation environment printf returns value which is always ignored palindrome used ( main2.c(9) ), but not defined $ lint main2.c reverse.c palindrome.c ---> check all modules together. main2.c: min2.c(11): warning: main() returns random value to invocation environment reverse.c: palindrome.c: Lint pass2: printf returns value which is always ignored $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  When you’re done: STRIP - The debugger and profiler, utilities both require that you compile a program using special options, each of which adds code to the executable file. remove this extra code after debugging and profiling are done with, Utility: strip { fileName }+ strip removes all of the symbol table, relocation, debugging, and profiling information from the named file(s).

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  When you’re done: STRIP - an example of how much space you can save by using strip: $ ls -l main2---> look at original file. -rwxr-xr-x 1 gglass 5904 Jan 8 22:18 main2* $ strip main2---> strip out spurious information. $ ls -l main2---> look at stripped version. -rwxr-xr-x 1 gglass 3373 Jan 8 23:17 main2* $ _

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone: mS[ 1] : close(0) OK 27mS[ 1] : open("try.in", O_RDONLY, ) 29mS[ 1] : END-open() = 0 29mS[ 1] : read(0, "1\n2\n|/bin/date\n3\n|/bin/sleep 2", 2048) = 31 29mS[ 1] : read(0, 0x7fff26ef, 2017) = 0 29mS[ 1] : getpagesize() = mS[ 1] : brk(0x1001c000) OK 29mS[ 1] : time() = mS[ 1] : fork() 31mS[ 1] : END-fork() = mS[ 1] ( ): was sent signal SIGCLD 31mS[ 2] : waitsys(P_ALL, 0, 0x7fff2590, WTRAPPED|WEXITED, 0) 42mS[ 2] : END-waitsys(P_ALL, 0, {signo=SIGCLD, errno=0, code=CLD_EXITED, pid= , status=0}, WTRAPPED|WEXITED, 0) = 0 42mS[ 2] : time() = Tracing System Calls Most operating systems contain a utility to monitor system calls: –HPUX: tusc, MAC OS X: ktrace, Linux: strace, Solaris: truss, SGI: par

C Programming Tools Prof. Andrzej (AJ) Bieszczad Phone:  CHAPTER REVIEW In this chapter, we learned how to:  Compile C programs  manage the compilation of multimodule programs  maintain archives  maintain multiple versions of source code  profile executable files  debug executable files