Separate Compilation
Our program is split up into two files.
- this is chemlib.h, it contains only the struct definitions,
and any const declarations, and prototypes for functions that we want the customer to
be able to use (in class I forgot to include the prototype for operator<<). A dot-h
file should never contain any real code.
- this is chemlib.cpp, it has a #include for "chemlib.h", and
contains only the definitions for the functions and methods promised by the dot-h file.
It does not contain a main().
- this is test.cpp, it has a #include for "chemlib.h", and
contains only our test code, with main.
We partially compile the library with this command
CC -c chemlib.cpp
This creates a file called chemlib.o, which is not human readable, and it is prohibitively
expensive to reverse engineer it to discover anything close to the original C++. Only
big companies can steal your work this way.
Now to make sure everything still works, we compile the test code and library together:
CC test.cpp chemlib.o
That produces a normal a.out which we can run and test.
We deliver only the following files to the customer:
The customer writes their own program, cust.cpp that uses our product,
and compile and run it happily:
CC cust.cpp chemlib.o
a.out
All without the customer ever being able to see how we made it work.