Programming and Graphics

(Kiana) #1

4.4 Functions in individual files 101


The content of the header filegreetings.his:

#ifndef GREETINGSH

#define GREETINGSH

#include<iostream>
using namespace std;
void greetings();

#endif

The “if not defined” loop checks whether the variableGREETINGSHhas been
defined. If not, the enclosed block of commands is executed:



  • The first of these commands defines the variableGREETINGSH,sothatthe
    loop will not be executed if the header file is linked for a second time. By
    convention, the nameGREETINGSHarises by capitalizing the name of the
    header file and then appending H.

  • The rest of the statements in the “if not defined” loop duplicate the
    function preamble and declaration.


The overall procedure ensures that a function is not defined multiple times.


A makefile that compiles the individual files and links the object files to
form an executable namedgreetingsreads:


OBJ1 = greetingsdr.o greetings.o

greetings: $(OBJ1)
c++ -o greetings $(OBJ1)
greetingsdr.o: greetingsdr.cc
c++ -c greetingsdr.cc
greetings.o: greetings.cc
c++ -c greetings.cc


  • The first line defines the variableOBJ1as the union of two object files.
    This variable is subsequently cited as$(OBJ1).

  • The second line states that the executable depends on$(OBJ1).

  • The third line performs the linking.

  • The last four lines compile the individual source files and generate the
    object files.


The white space in the third, fifth, and seventh lines must be generated by
pressing theTabkey in the text editor.

Free download pdf