Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.

Similar presentations


Presentation on theme: "Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated."— Presentation transcript:

1 Program in Multiple Files

2 l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated by the compiler n examples: assignment statements, looping/branching constructs, function invocations l non-executable - no machine code generated examples: function prototypes, global variable and constant declarations, #include directives l global constant declarations may look like executable - they are not: const double PI=3.14; the compiler substituites 3.14 for ever occurrence of PI in the program (Non) Executable Statements

3 #include instructions tell the compiler to include specified file. The files included are called header files and commonly have extensions.h l two forms: #include - the file is found in standard system- dependent location #include “filename.h” - the file is located in the same directory as the rest of the code l the include directives are processed before the rest of the compilation l header files may also contain include directives l what to put in include files - non-executable statements l what not to put in include files - executable statements, function definitions l purpose of header files - centralize declarations Include Files

4 l large programs are usually kept in multiple files l reasons: n easy to maintain n can be compiled separately l functions are usually grouped into files by their purpose (functions dealing with one particular part of program are kept in one file) l function invocations, constants and variables cannot be put in program before the corresponding declarations. n what if they are in a separate file? l Program is structured as follows: n program file (extension.cpp) - contains function definitions n header file (extension.h) - contains corresponding function prototypes, global constant and variable declarations If function A defined in file AA.cpp needs to call a function B which is defined in a different file BB.cpp - the corresponding header file BB.h is include d in file AA.cpp Program in Multiple Files

5 // adds one int add1(int); Example Program in Multiple Files // uses the function add1 // defined in a separate file #include #include "add1.h" int main() { // get the number cout << "Enter a number: "; int n; cin >> n; // find the number plus 1 int newn = add1(n); // print out the number plus 1 cout << newn << endl; } add1test.cpp add1.h #include "add1.h" // adds 1, // returns added value int add1(int n) { return (n + 1); } add1.cpp

6 compilation of program is multiple files is done in 2 stages l compiling source files n each source file is compiled to produce an object file. option “-c” tells compiler to stop at object file and do not produce executable: g++ -c add1.cpp produces object file add1.o l producing executable n object files and predefined functions (from standard libraries) are linked to produce the executable: g++ add1test.o add1.o -o add1test Separate Compilation

7 Add include files Executable program Source program (add1.cpp) Check file unit for legal syntax and compile it into an object file Link object file with standard object files and other object files to produce an executable program Include files (add1.h, iostream) compilation Object file (add1.o) Separate compilations Standard libraries

8 l make utility is designed for separate compilation of programs l every goal has: n target file (or name of the goal) - file to be produced when this goal is executed n set of commands to execute n set of files this goal depends on - files to be used in the execution of the goal l before executing a goal make checks if the target’s last modification time is earlier than the times of files it depends on; if not - the goal is not executed; if yes, the goals for the dependents are checked in the recursive manner l for separate compilation there are usually the following goals: n goal for linking (producing an executable) - depends on objects files that are used to create executable n goals for producing object files (compiling) - depends on source and header files Makefile with Multiple Files

9 Example Makefile # Makefile for add1 program # executable # add1test : add1test.o add1.o g++ -g add1test.o add1.o -o add1test add1test.o : add1test.cpp add1.h g++ -g -c add1test.cpp add1.o : add1.cpp add1.h g++ -g -c add1.cpp clean : rm -f add1test.o add1.o add1test

10 l In emacs and xxgdb load correct files to show tracing l some gdb commands useful for multiple file programs break filename.cpp:9 - sets a breakpoint on line 9 in file filename.cpp break funcname - sets a breakpoint at the beginning of function funcname() regardless of the file it is located in info breakpoints - lists all breakpoints set in the program delete 5 - deletes a breakpoint by the number listed in info (deleted 5th breakpoint) clear filename.cpp:5 or clear funcname - deletes the breakpoints by location in the program Gdb with Multiple Files

11 l each definition (including structure definition) can be encountered only once during compilation. l When structure definition is placed in a header file, must be careful it is not read twice n mechanism - preprocessor directives #define name value note that substitution is textual problem: #define press 50 int myvar = press + impress; –first variable and part of second will be substituted #ifdef name - true if name defined, #ifndef name - true if not #endif - completes #if header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif Preprocessor Directives


Download ppt "Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated."

Similar presentations


Ads by Google