2

I wonder why recommended way is to #include<example.hpp> in example.cpp; I don't understand, how preprocessor (which seems to be quite simple program) knows that definition of methods are in example.cpp - name of this file isn't appear in main.cpp and in included file example.hpp. AFAIK, #include<filename> is simply replaced using filename's content.

I always invert the situation - I #include<example.cpp> in example.hpp. Then, preprocessor, while including example.hpp includes also example.cpp.

5
  • 4
    Hope that works out for you when you start violating the One Definition Rule. Commented Aug 18, 2014 at 18:01
  • 2
    You won't be able to reuse the example.hpp anywhere else besides your main.cpp if you include the corresponding source there. Stop that nonsense please! Immediately! Commented Aug 18, 2014 at 18:05
  • 1
    stackoverflow.com/questions/333889/… Commented Aug 18, 2014 at 18:06
  • 3
    This question shows a misunderstanding of how the C++ compilation process works. It's a completely reasonable misunderstanding though, so I'm not sure why the downvotes. Commented Aug 18, 2014 at 18:11
  • 1
    @πάνταῥεῖ: He's not asking about a problem in any specific piece of code, so an MCVE makes no sense here. Commented Aug 18, 2014 at 18:19

1 Answer 1

3

You can #include arbitrary files in a C++ translation unit (the *.cpp you are compiling) provided the preprocessed form (after pre-processing) is valid C++. Read some documentation on the preprocessor and the C preprocessor wikipage. Don't forget that preprocessing is the first phase in a C or C++ compiler. (Historically, it was even a different process /lib/cpp; now it is inside the compiler for performance reasons).

The name of the included file does not matter much. Conventionally, you don't want to name an included file to look like a top-level translation unit. This is why people generally do not #include "example.cpp" but something like e.g. #include "example.inc" or #include "example-inc.cpp" (or #include "example.def", specially if you #include several times a header). The standard C++ library accepts #include <map> for example.

See for example this answer which shows some included file which is #include-d several times (for different purposes), or, inside the GCC source tree, the file gcc/tree.def

Of course, you cannot #include several times an arbitrary C++ file in the same compilation unit (because you cannot have several definitions of e.g. the same function).

In practice, use e.g. g++ -C -E foo.cc > foo.ii to get the pre-processed form of translation unit foo.cc ... then look with a pager or editor into the preprocessed form foo.ii

Read also about some proposal on modules in C++. It was not accepted in the latest C++ standard, but something similar might perhaps become standardized in the future.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.