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.
define.C,
define.txt.
PI.
Traditional to give an uppercase name to a macro.
The
#define
directive tells the C++ preprocessor to change every subsequent
copy of the word PI
to
3.14159265358979.
const.C,
const.txt.
const
variable instead of a macro.
ifdef.C,
ifdef.txt.
Conditional compilation with
#ifdef.
.C
files.
It produces no output.
To compile a multi-file program,
c++ main.C file1.C ls -l a.out
.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
for the header file
(on our machine
storm.cis.fordham.edu).
#include
with
"double quotes"
looks in your current directory for the header file.
.C files,
but not the name of the .h
file:
c++ main.C file1.C
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 {
| ^~~~~
We could render this multiple inclusion harmless by changing
point.h
to this
point.h.
point.h
to be included more than once,
and the compiler would pay atention to it ony the first time it is included.