Program in Multiple Files Non Executable Statements l
Program in Multiple Files
(Non) Executable Statements l l all C++ statements are divided into executable and non-executable - some corresponding machine code is generated by the compiler n examples: assignment statements, looping/branching constructs, function invocations non-executable - no machine code generated n examples: function prototypes, global variable and constant declarations, #include directives 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
Include Files l l l l #include instructions tell the compiler to include specified file. The files included are called header files and commonly have extensions. h two forms: #include <filename> - the file is found in standard systemdependent location #include “filename. h” - the file is located in the same directory as the rest of the code the include directives are processed before the rest of the compilation header files may also contain include directives what to put in include files - non-executable statements what not to put in include files - executable statements, function definitions purpose of header files - centralize declarations
Program in Multiple Files l l l large programs are usually kept in multiple files reasons: n easy to maintain n can be compiled separately functions are usually grouped into files by their purpose (functions dealing with one particular part of program are kept in one file) function invocations, constants and variables cannot be put in program before the corresponding declarations. n what if they are in a separate file? 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 included in file AA. cpp
Example Program in Multiple Files add 1. h // adds one int add 1(int); add 1. cpp #include "add 1. h" // adds 1, // returns added value int add 1(int n) { return (n + 1); } add 1 test. cpp // uses the function add 1 // defined in a separate file #include <iostream> #include "add 1. h" int main() { // get the number cout << "Enter a number: "; int n; cin >> n; // find the number plus 1 int newn = add 1(n); // print out the number plus 1 cout << newn << endl; }
Separate Compilation 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 add 1. cpp produces object file add 1. o l producing executable n object files and predefined functions (from standard libraries) are linked to produce the executable: g++ add 1 test. o add 1. o -o add 1 test
Separate Compilation Source program (add 1. cpp) Add include files Include files (add 1. h, iostream) Check file unit for legal syntax and compile it into an object file compilation Separate compilations Object file (add 1. o) Standard libraries Link object file with standard object files and other object files to produce an executable program Executable program
Makefile with Multiple Files l l make utility is designed for separate compilation of programs 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 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 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
Example Makefile # Makefile for add 1 program # executable # add 1 test : add 1 test. o add 1. o g++ -g add 1 test. o add 1. o -o add 1 test. o : add 1 test. cpp add 1. h g++ -g -c add 1 test. cpp add 1. o : add 1. cpp add 1. h g++ -g -c add 1. cpp clean : rm -f add 1 test. o add 1 test
Gdb with Multiple Files l l In emacs and xxgdb load correct files to show tracing some gdb commands useful for multiple file programs break filename. cpp: 9 - sets a breakpoint on line 9 in 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 5 th breakpoint) clear filename. cpp: 5 or clear funcname - deletes the breakpoints by location in the program
Preprocessor Directives l l l each definition (including structure definition) can be encountered only once during compilation. 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 n 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
- Slides: 11