x
that needs to be resolved, and it finds the definition of a variable named x
,
it will resolve the reference, even if the reference is using the variable as an integer and the definition was for a double. (This may
be a bit murky at the moment, but you get the general idea — the types of the reference and definition don't match).
x = y / z;the compile generates a different sort of division operation depending upon whether the two operands were both declared as integers or whether one of them is a double; furthermore, it would generate an error if these are strings.
x, y
, and z
.
main
as the first function, we introduce function headers that
are placed before main
so that the compiler can see the 'type' of the function and make sure it is being called correctly.
#include
comes in
#
symbol
#define
: introduces a macro — a fragment of code that is assigned a name:
#define PI 3.1415 #define NUM_ELEMENTS 100
double diameter = …; double circumference = diameter * PI;
#define TWICE(x) 2 * x // dangerous (wrong?)!
()
's
#if/#elif/#else/#endif
: introduces a (preprocessor-time) conditional that return true if the condition
is true (the condition must be one that can be evaluated by the preprocessor, and is typically a macro,
or an expression involving macros)
#define DEBUG 0 … #if DEBUG cout << "The value of i is << i << endl; #endif
#ifdef/#ifndef
: evaluates to true (false) if the condition (which is a macro) has been defined
#define MACOS #ifdef MACOS … #else … #endif
#include
instructs the preprocessor to retrieve the contents of the specified file
and physically paste it into the source file at the point where the #include
appears.
#include
array_utils
)
#include
.h
suffix.
array_utils.h
).
using namespace ...
in header files
#ifndef FILENAME_H #define FILENAME_H #defines constants function headers ... #endif
#include
the corresponding header file
standard library #includes user-defined #includes using namespace std; #define's constants variables function headers functions
.h
) file contains the declaration of the external funtion — there must also be a definition of that function (for the linker to
be able to use when resolving references to the function).
.cpp
(rather than #include
of the corresponding
header (.h
) file in the implementation (.cpp
) file.
.h
file's
prototypes are in synch with the protoypes of the actual function definitions
g++
actually invokes several programs in its translation of a source file to an executable:
g++
attempts to build a complete
executable from the files specified on its command line. To do this it:
.o
) file.
a.out
) file.
g++
will set them aside, compile any source files and then submit all object files — new and pre-existing to the linker.
g++
to limit which phases are actually executed:
-E
causes only the preprocessor to run, sending the file resulting from the header file expansions
to be sent to standard output (you will have little reason to use this option &mdashl it's one of the exercises
below simply so you can see the effects of the preprocessor).
-c
stops after the compilation phase, retaining the resulting object file; no link is performed.
main
to speak of and thus performing a line would prodcue an undefined symbol error (every executable
must have a main
; it is the linker that generates the no main
error when it fails to find a definition of main
).
.h
files are for use by the compiler only-- they contain no executable code, but rather declarations that allow the
compiler to check for (type) errors and generate code.
.h
file on the g++
command line — one does not compile such files
into an object file; rather they are included in source files for use during compilation.