As far as I know, the C++ preprocessor is responsible for replacing
#include statements with the contents of the actual files. With my
version of gcc, this seems to be the only rule. If you were to do
something stupid like this
(contents of file named recurs.cc:)
#include <iostream>
#include "recurs.cc"
void main() {
cout << "recursive!" << endl;
}
the preprocessor keeps doing a recursive include until it gets an
error. On my system [gcc version 2.95.4 20011002 (Debian prerelease)]
compilation of this eventually resulted in this error:
recurs.cc:2: macro or `#include' recursion too deep
So, to answer your question, you can duplicate things as much as you
like as long as you are careful. If the same file is included more
than once and it declares variables or functions the compiler will
return a variable already defined error. The standard libraries are
generally not a problem because they define flags that keep the
contents of the file from being used more than once. Math.h is
probably surrounded by something like
#ifndef __MATH_H__
#define __MATH_H__
// contents of math.h here
#endif
This ensures that no matter how many times math.h is included in a
file, it will only get read once, since on subsequent passes
__MATH_H__ will be defined (#ifndef means process the section until
#endif only if the variable following is not defined). |