point stuff.
Furthermore, the point stuff falls into two obvious
pieces:
point.h)
point.cpp)
Vector definition and the main
function (main.cpp).
point.h -- Prototypes for point stuff |
point.cpp -- Definitions for point-related functions | ||
|
main.cpp -- New code with definition for |
Vector and
main
|
|
point and all of its
related functions, we really only need the function prototypes and struct
definition - the definitions of the functions don't really
concern us as users of point. We only care about
what's available for us to use, not how it works. So, the file
has the line:
#include "point.h"
which means literally pretend that the entire contents of
point.h was typed in starting at this point.
Thus, as far as the compiler is concerned, it's like that struct
definition and all those prototypes were right there.
point.cpp
also begins with #include "point.h", since those
function definitions won't make sense to the compiler without
the definition of the struct point and the prototypes.
g++ main.cpp point.cpp -o vectorNote: No .h files should be in the g++ compling command.
.h file (or header file) that
gives the outside world all the information it needs to use it.
.cpp file that contains the source code that implements the
it (i.e. that tells the compiler how all these functions really work).
point library.
One key thing that's missing from my simple point library, however, is documentation. The header file needs a lot of documentation so that the user of the module understands how to use the struct and the various functions the module offers.
main.cpp into two parts: Vector and
main().
| vec.h | main2.cpp |
| |
$ g++ point.cpp main2.cpp -o vector
In file included from vec.h:1,
from main2.cpp:4:
point.h:7:8: error: redefinition of ‘struct point’
7 | struct point
| ^~~~~
In file included from main2.cpp:3:
point.h:7:8: note: previous definition of ‘struct point’
7 | struct point
| ^~~~~
The problem is that this causes the struct point to be defined
twice in main2.cpp. Why?
main2.cpp #includes point.h.
This gives the first definition of point in main2.cpp
main2.cpp also #includes
vec.h.
vec.h also #includes
point.h! This amounts to the second definition of point in
main2.cpp!
.h files
with the following:
#pragma onceIt is widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. The preprocessor is invoked by the compiler as the first part of translation. One of the jobs of the preprocessor is taking care of inclusion of header files.
| point.h | vec.h |
|
|
$ g++ point.cpp main2.cpp -o vector
$
Makefile (notice how there's no file extension).
| Makefile |
vector: main2.cpp point.cpp point.h vec.h g++ point.cpp main2.cpp -o vector |
Makefile in my directory, I need only run the following
in a terminal.
$ make vector
Then, it will find that command vector in
the Makefile and run the corresponding g++ command.
vector
is sensitive to.
make
vector will run the g++ command again. In other words, files
changed and they should be re-compiled.
make vector won't do anything.
In other words, files haven't changed and there is no need to re-compile the source code.
Assume that your created Makefile as above and compiled the source code by
running make vector.
make vector again. What will happen?
vec.h and run make vector
again. What will happens?
vec.h and run make vector again. What will happen?