program in multiple files. l all c++ statements are divided into executable and non-executable l...
DESCRIPTION
#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 FilesTRANSCRIPT
Program in Multiple Files
all C++ statements are divided into executable and non-executable executable - some corresponding machine code is generated by the
compiler examples: assignment statements, looping/branching constructs,
function invocations non-executable - no machine code generated
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
(Non) Executable Statements
#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 system-
dependent 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
Include Files
large programs are usually kept in multiple files reasons:
easy to maintain 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. what if they are in a separate file?
Program is structured as follows: program file (extension .cpp) - contains function definitions 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
Program in Multiple Files
// adds oneint add1(int);
Example Program in Multiple Files
// uses the function add1// defined in a separate file#include <iostream>#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 valueint add1(int n) { return (n + 1);}
add1.cpp
compilation of program is multiple files is done in 2 stages compiling source files
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 producing executable
object files and predefined functions (from standard libraries) are linked to produce the executable:g++ add1test.o add1.o -o add1test
Separate Compilation
Separate Compilation
Add include files
Executable program
Source program(add1.cpp)
Checkfile
unit for legalsyntax and
compile it intoan object file
Link object filewith standard
object filesand other
object files toproduce anexecutableprogram
Include files(add1.h, iostream)
compilation
Object file
(add1.o)
Separate compilations
Standard libraries
make utility is designed for separate compilation of programs every goal has:
target file (or name of the goal) - file to be produced when this goal is executed
set of commands to execute 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: goal for linking (producing an executable) - depends on objects
files that are used to create executable goals for producing object files (compiling) - depends on source
and header files
Makefile with Multiple Files
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
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 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 programdelete 5 - deletes a breakpoint by the number listed in info
(deleted 5th breakpoint)clear filename.cpp:5 orclear funcname - deletes the breakpoints by location in the
program
Gdb with Multiple Files
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 mechanism - preprocessor directives#define name value note that substitution is textual problem: #define press 50int 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