c++ - Why #include<.hpp> in .cpp, not <.cpp> in .hpp? -
this question has answer here:
- how compilation/linking process work? 5 answers
- why have header files , .cpp files? [closed] 9 answers
i wonder why recommended way #include<example.hpp> in example.cpp; don't understand, how preprocessor (which seems quite simple program) knows definition of methods in example.cpp - name of file isn't appear in main.cpp , in included file example.hpp. afaik, #include<filename> replaced using filename's content.
i invert situation - #include<example.cpp> in example.hpp. then, preprocessor, while including example.hpp includes example.cpp.
you can #include arbitrary files in c++ translation unit (the *.cpp compiling) provided preprocessed form (after pre-processing) valid c++. read documentation on preprocessor , c preprocessor wikipage. don't forget preprocessing first phase in c or c++ compiler. (historically, different process /lib/cpp; inside compiler performance reasons).
the name of included file not matter much. conventionally, don't want name included file top-level translation unit. why people not #include "example.cpp" e.g. #include "example.inc" or #include "example-inc.cpp" (or #include "example.def", specially if #include several times header). standard c++ library accepts #include <map> example.
see example this answer shows included file #include-d several times (for different purposes), or, inside gcc source tree, file gcc/tree.def
of course, cannot #include several times arbitrary c++ file in same compilation unit (because cannot have several definitions of e.g. same function).
in practice, use e.g. g++ -c -e foo.cc > foo.ii pre-processed form of translation unit foo.cc ... pager or editor preprocessed form foo.ii
read some proposal on modules in c++. not accepted in latest c++ standard, similar might perhaps become standardized in future.
Comments
Post a Comment