cpp sourcefile without #include headers?

hey,
I've recently worked on a project where i've seen multiple .cpp source files without any header #includes.
They have a .h files where everything is declared but they never #include it. So is it somehow possible through the ide, make or whatever
to create global includes without ever writing a #include?
If the .cpp file doesn't have a #include statement, then it won't include any header files. Doesn't matter if the IDE has created "global includes" - whatever they are.
that's what I was thinking as well, but it does use the header classes and defines the class functions etc.
It is very common.

The purpose of a header file is to make things visible to the user of a library.

The library itself may be made up of as many .cpp files as you wish. Every .cpp does not need a header.


A common idiom is also to have an “internal” header for declaring stuff the .cpp files need to know about each other but that the external user of the library does not need to know (and/or should not know).
hmm interesting, I haven't seen this in my career so far and worked with many libs, but always had to include their headers, as far as I remember
So where do these source files get their definitions from, if they're not including the header files that contain the definitions?

Are they being duplicated in the source files? (Which would be a maintenance nightmare.)

Or are the source files themselves being included somewhere else that also includes the header files? (Which would be weird.)
Last edited on
1
2
3
4
5
6
7
int foo() {
  return 42;
}

int bar( int x ) {
  return x + foo();
}

The foo is implicitly declared by its implementation and can thus be called by bar. Presuming this is in a library, the user would include a header with the plain declarations.


As long as there is just functions and no inter-source dependencies ...
> They have a .h files where everything is declared but they never #include it.

This is truly terrible programming practice (unless these are definitions of templates and the cpp file itself is included at the end of the header file). Amateurs may do this; assuming that this is production code, how on earth did such a component pass a code review?

See: SF.5: A .cpp file must include the .h file(s) that defines its interface
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-consistency

In addition, the #include of the header file that defines its interface should be before any other other #include and before any other declarations. This will allow the compiler to verify that the header is self-sufficient (that it is compilable; that a user who includes the header file won't get the nasty surprise of compile-time errors.)
with they include everything i mean of course only stuff that should be in the class.
so that should be ok, for me it's still magic how the cpp knows about all the stuff.

MyObject.h

has all the declarations

MyObject.cpp

has all the definitions for the MyObject.h
but MyObject.cpp doesn't have any includes, but it finds the class name space and everything.

Idk how this really work.
Last edited on
> MyObject.cpp doesn't have any includes, but it finds the class name space and everything.
> Idk how this really work.

Have a look at the make file. It is possible by using a preprocessor option.

For instance, with the GNU compiler: -include MyObject.h
https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#Preprocessor-Options
It seems like we use precompiled header files, and we use Jam build tool, maybe the recompiled header is added automatically this way
Registered users can post here. Sign in or register to post.