Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:

Similar presentations


Presentation on theme: "Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:"— Presentation transcript:

1 Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site: http://www.cs.fsu.edu/~asriniva/courses/aup-02 http://www.cs.fsu.edu/~asriniva/courses/aup-02 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan

2 Announcements Collected your quizzes –The grade on this will not count towards your course grade –However, it will provide you feedback on your C knowledge –You can try the linked list question at home You should be able to write the code within 15 minutes, without referring to any book You should have received an email from me, if you are registered for this course

3 Week 1 Topics Course outline and policies UNIX programming environmentUNIX programming environment –Editors –C compilers and debuggers –Makefiles Review some features of CReview some features of C –Header files –Command line arguments –Utilities Review some UNIX system calls –system, etc

4 UNIX programming environment EditorsEditors C compilersC compilers DebuggerDebugger MakefilesMakefiles –make

5 vi, pico, emacs, etc What is good about emacs? –emacs is more than just an editor –You can compile and edit within emacs –If you know lisp, you can expand its functionality –Some useful commands to get started: C-h t get tutorial C-g cancel command C-x C-c quit emacs C-h help Editors

6 etags This helps you go to a function definition –Particularly useful in programs spanning multiple files –At the shell prompt, type etags *.[ch] –In emacs Choose visit tags from the _C_ menu –Or ESC-x visit-tags-table Subsequently, find tag from the menu, or ESC-., will take you to the function definition

7

8 Compiling in emacs Choose compile from the _C_ menu –Or ESC-x compile –Errors and warnings will be printed on a sub-window –CTL-X ` will take you to the next error or warning

9 gcc, cc Using ANSI C, the code must pass with flags –Wall –ansi –pedantic with no warning messages See example1.cexample1.c –How can we fix warning? Some examples –gcc –g –Wall –ansi –pedantic example1.c –gcc –g –c –Wall –ansi –pedantic example1.c –gcc –g example1.o –gcc –g example.o -lm C compilers

10 ddd, xxgdb, gdb The code must be compiled with –g option. The power of a debugger: –Finding the line that causes core dump –See example1.c Break point, show value, change value, step, next, continue, print –Very efficient in debugging sequential code –Not very effective in debugging concurrent code (multiple threads, multiple processes) Good software development practice: You must have seen each line of your code execute in the debugger, at least once Debugger

11 run to run the program

12 make [-f makefile][option] target –A tool to update files derived from other files –The default files for make are./makefile,./Makefile,./s.makefile –Use the –f option to specify some other file make –f makefile1 –The makefile has three components Macros: define constants Target rules: Specify how targets are made Inference rules: Specify how targets can be made, implicitly. make will first check if a target rule applies, before using inference rules. Make

13 Macros: String1 = string2. –Example CC=gcc CFLAG=-Wall –ansi –pedantic Target rules: Target : [prerequisite…] command –Example a.out : myprog1.c myprog2.c myprog3.c $(CC) $(CFLAG) myprog1.c myprog2.c myprog3.c make... continued

14 Inference rules Target: command Target must be of the form.s1 or.s1.s2 where.s1 and.s2 must be prerequisites of the.SUFFIXES special target. –.s1.s2  make *.s2 from *.s1 –.s1  make * from *.s1 Example:. c: $(CC) –o $@ $<.c.o: $(CC) –c $< make... continued

15 See the example makefiles –makefile, makefile1, makefile2makefilemakefile1makefile2 makefile1 will recompile only the modified files, instead of everythingmakefile1 makefile2 has inference rulesmakefile2 www.cs.fsu.edu/cgi-bin/man.cgi can be used to find more informationwww.cs.fsu.edu/cgi-bin/man.cgi makefile examples

16 Review some features of C Header filesHeader files MacrosMacros Command line arguments Utilities

17 Header files Usually define interfaces between separately compiled modules May contain macro definitions, preprocessor directives, declarations of types, and function prototypes Should not contain variable definitions or executable code

18 Some header file errors Improper header file use can cause problems –Try compiling example2.c –Including a header file multiple times may cause redefinition errors –Why does including stdio.h twice not cause any problem? Look at /usr/include/stdio.h

19 Conditional Code in Headers Preprocessor directives are used to prevent the body of a header file from being used multiple times. #ifndef MYHEADER #define MYHEADER /* the body of the header file */ #endif

20 Macros with and without Parameters #define MAX_LENGTH 256 –... for (i = 0; i < MAX_LENGTH; i++)... Macros can have parameters –#define max(a,b) (a > b) ? a : b What is wrong with the following? –#define sum(a, b) a + b –#define product(a, b) a*b –See example3.c, example3b.c, example3c.c, and example3d.c


Download ppt "Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 2 Class web site:"

Similar presentations


Ads by Google