c++ - Why #include<.hpp> in .cpp, not <.cpp> in .hpp? -


this question has answer here:

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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -