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.
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).
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?
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.