When you give the
c++
command to translate your C++ program into terms the computer can understand
and execute,
the
c++
command begins by running the C++ preprocessor.
The C++
preprocessor
is a text editor that automatically edits the C++ program.
The lines that begin with
#
are
directives
that tell the preprocessor what to do.
const.C,
const.txt
const
variable.
define.C,
define.txt
PI.
#define
directive tells the C++ preprocessor to change every subsequent
copy of the word
PI
to
3.14159265358979.
#ifdef
(“if the macro is defined”)
and
#endif
#ifndef
(“if the macro is not defined”)
and
#endif
.C
files.
To compile and run a multi-file program,
c++ -o ~/bin/point main.C file1.C (minus lowercase O space tilde) ls -l ~/bin/point point point p is (3, 4) etc.
.C
files and one .h
(header) file.
It produces no output.
When you write an
#include
directive,
#include
with
<angle brackets>
looks in the directory
/usr/include/c++/15
(on our machine
storm.cis.fordham.edu)
for the header file.
#include
with
"double quotes"
looks in your current directory for the header file.
.C files,
but not the name of the .h
file:
c++ -o ~/bin/point main.C file1.C ls -l ~/bin/point point point p is (3, 4)
main.C
file accidentally included the header file twice?
//In main.C. No reason to do this. #include "point.h" #include "point.h"
c++ main.C file1.C
In file included from main.C:4:
point.h:1:8: error: redefinition of ‘struct point’
1 | struct point {
| ^~~~~
In file included from main.C:3:
point.h:1:8: note: previous definition of ‘struct point’
1 | struct point {
| ^~~~~
point.h
to this
point.h.
point.h
to be included more than once,
and the compiler would pay attention to it ony the first time it is included.