The Preprocessor CS 2022 Introduction to C Instructor Hussam Abu Libdeh Cornell University based on slides by Saikat Guha Fall 2009 Lecture 8 The Preprocessor CS 2022 Fall 2009 Lecture 8 Preprocessor I I I Commands to the compiler Include files shortcuts conditional compilation Command must start at beginning of line Common preprocessor commands I I I include define ifdef ifndef The Preprocessor CS 2022 Fall 2009 Lecture 8 Running just the preprocessor gcc E o preprocessed c project c The Preprocessor CS 2022 Fall 2009 Lecture 8 include Header Files I I Includes files Literally copy paste Typically header files Header File Declares I External functions I Variable types I External global variables Typically named h or sometimes hpp for C The Preprocessor CS 2022 Fall 2009 Lecture 8 include Header Files mylib h int max int a int b mylib c include mylib h int max int a int b return a b a b The Preprocessor CS 2022 Fall 2009 Lecture 8 include Header Files project c include mylib h void foo m max p q gcc o project project c mylib c The Preprocessor CS 2022 Fall 2009 Lecture 8 define Macros Blind substitution inside file define define malloc maxsize mymalloc 100 p malloc maxsize printf Allocated d bytes maxsize is exactly the same as p mymalloc 100 printf Allocated d bytes 100 The Preprocessor CS 2022 Fall 2009 Lecture 8 ifdef Conditional compilation project c ifdef DEBUG include mylib h define malloc define free endif mymalloc myfree p malloc 100 For debugging gcc DDEBUG o project project c mylib c For release gcc o project project c mylib c The Preprocessor CS 2022 Fall 2009 Lecture 8 ifdef Conditional compilation mylib h void mymalloc int size void myfree void ptr The Preprocessor CS 2022 Fall 2009 Lecture 8 ifdef Conditional compilation mylib c include stdio h include stdlib h void mymalloc int size void ret malloc size fprintf stderr Allocating d at p n size ret return ret void myfree void ptr fprintf stderr Freeing p n ptr free ptr The Preprocessor CS 2022 Fall 2009 Lecture 8 include Problems mylib1 h include mylib2 h mylib2 h include mylib1 h The Preprocessor CS 2022 Fall 2009 Lecture 8 include Solution mylib1 h ifndef MYLIB1 H define MYLIB1 H include mylib2 h endif mylib2 h ifndef MYLIB2 H define MYLIB2 H include mylib1 h endif The Preprocessor CS 2022 Fall 2009 Lecture 8 define More useage project c define prod a b prod2 a b 10 prod 5 6 prod2 5 6 10 prod 5 6 7 prod2 5 6 7 10 BUG The Preprocessor CS 2022 Fall 2009 Lecture 8 define Solution project c define prod a b prod2 a b 10 prod 5 6 7 prod2 5 6 7 10 The Preprocessor CS 2022 Fall 2009 Lecture 8 define More useage project c define oldfunc a b newfunc1 a newfunc2 b oldfunc 5 6 newfunc1 5 newfunc2 6 for i 0 i 5 i oldfunc 5 6 for i 0 i 5 i newfunc1 5 newfunc2 6 BUG The Preprocessor CS 2022 Fall 2009 Lecture 8 define Solution project c define oldfunc a b do newfunc1 a newfunc b while 0 for i 0 i 5 i oldfunc 5 6 for i 0 i 5 i do newfunc1 5 newfunc2 6 while 0 The Preprocessor CS 2022 Fall 2009 Lecture 8 define More problems project c define max a b a b a b max p q p q p q max f1 f2 f1 f2 f1 f2 BUG Solution Be extra careful when calling a function inside code that could be a define Always use uppercase for macros to serve as reminder The Preprocessor CS 2022 Fall 2009 Lecture 8
View Full Document
Unlocking...