Programming In C Spring Semester 2013 Lecture 11

  • Slides: 9
Download presentation
Programming In C++ Spring Semester 2013 Lecture 11 Programming In C++, Lecture 11 By

Programming In C++ Spring Semester 2013 Lecture 11 Programming In C++, Lecture 11 By Umer Rana

Larger Programs Separate Compilation These files can be written, compiled separately and then linked

Larger Programs Separate Compilation These files can be written, compiled separately and then linked together to form the final executable program. e. g. void main() { int a, b, ans; printf(“Type two integersn”); scanf(“%d %d”, &a, &b); ans= formula(a, b); printf(“Total of two integers are: %d”, ans); } Programming In C++, Lecture 11 By Umer Rana

Larger Programs Separate Compilation These files can be written, compiled separately and then linked

Larger Programs Separate Compilation These files can be written, compiled separately and then linked together to form the final executable program. e. g. int formula(int x, int y) { return (x*x + y*y); } Programming In C++, Lecture 11 By Umer Rana

Project Features • The Process can either be done “by hand”, by typing command-line

Project Features • The Process can either be done “by hand”, by typing command-line messages to compile each source file and to link them together or process can be turned over to a Make utility. • Make is a program that automates the compiling and linking process. When we have a large program with dozen source files we need to compile each one, then link the resulting. obj files to create a final exe. file. • If we change the source file, we need to recompile it and relink everything, but no need to recompile these file that haven’t changed. it is difficult to keep track of which have been compile since the last link and which haven't Programming In C++, Lecture 11 By Umer Rana

Project Features • Make utility keeps track of which source files must be recompiled

Project Features • Make utility keeps track of which source files must be recompiled to produce the final exe file. • Make utility automates the whole compile and link process, so that one command suffices to perform all the steps necessary to generate the final exe file. Programming In C++, Lecture 11 By Umer Rana

Advantages of Separate Compilation • Compile time is proportional to the size of the

Advantages of Separate Compilation • Compile time is proportional to the size of the file being compiled so minimize compile time. • Programming compile only those parts of a program that have been changed since the last compilation. • Already working & debugged are not recompile. • New file is compiled and linked with the previously compiled files. • Enables to programmers to write well-structured modular programs. • Separate compilation allows programmer to use library files. Programming In C++, Lecture 11 By Umer Rana

Conditional Compilation • The useful facility provided by the pre-processor is conditional compilation •

Conditional Compilation • The useful facility provided by the pre-processor is conditional compilation • The C preprocessor provides a better alternative, namely conditional compilation. Lines of source code that may be sometimes desired in the program and other times not, are surrounded by #if, # defined, #endif directive pairs as follows: Programming In C++, Lecture 11 By Umer Rana

Conditional Compilation #define TIMER int main () { int j, k; #if defined(TIMER) printf("Starting

Conditional Compilation #define TIMER int main () { int j, k; #if defined(TIMER) printf("Starting Testn"); #endif #if defined(TIMER) printf("Ending Testn"); #else printf("Donen"); #endif } getch(); Programming In C++, Lecture 11 By Umer Rana

Conditional Compilation #undef Directive • This #undef Directive cancels the action of a previous

Conditional Compilation #undef Directive • This #undef Directive cancels the action of a previous #define Directive. • This Directive to make conditional compilation specific to certain sections of the program. • Another use for #undef is to “turn off” macros that you have previously defined with a #define directive. e. g. #define TEST #undef TEST Programming In C++, Lecture 11 By Umer Rana