someprogram.cpp -----------> #include<string>
     |
     | #include"someheader.h"
     V
someheader.h --------------> #include<string>

what happens if I do this? Is it necessary to include the headers into all files that use them, or can you just go

#include<string>
#include...
#include"someheader.h"
#include"anotherheader.h"

and string and other required headers will be included in the ones below it as well?

Dani AI

Generated

Brief answer and practical rules (building on , , and ): the preprocessor places header text into each translation unit, so headers must be written so repeated insertion is harmless and so any TU that includes a header can compile that header by itself.

Always protect headers with an include guard or #pragma once:

#ifndef MY_HEADER_H
#define MY_HEADER_H

// declarations

#endif // MY_HEADER_H

Follow "include what you use." If a header exposes a type in its public declarations (for example a function parameter or a member type), that header must bring in the full declaration for that type so clients do not have to guess which other headers to include. If the header only mentions a type by pointer or reference, prefer a forward declaration to avoid dragging heavy headers into every TU:

// header.h
class Logger;   // forward declaration is fine for pointers/references

class Widget {
    Logger *logger;   // OK with forward decl
};

You should not rely on transitive includes (one header quietly including another) — that makes code fragile when authors change includes.

Avoid placing non-inline function definitions or non-const object definitions in headers; those cause multiple-definition linker errors. Use extern in headers and define the object in a single .cpp, or mark functions inline/use inline variables where appropriate.

Troubleshooting checklist:

  • redefinition errors: check guards and remove non-inline definitions from headers;
  • "no type named X": make sure the header includes the needed header or forward-declare if appropriate;
  • circular includes: break cycles with forward declarations or move includes into .cpp;
  • long compile times: minimize includes, or use precompiled headers.

Further reading: the preprocessor #include behaviour and the One Definition Rule are documented on cppreference and . For policy and trade-offs see Include What You Use.

Recommended Answers

All 11 Replies

You can derive the answer yourself from the fact that #include effectively copies the text of the header file into the cpp file before the compiler starts compiling.

So, my question is: why would you want to require people to follow extra instructions just to use your header file?

well, I knew that the include() function in PHP just copied the text from one file into the calling file, but I didn't know if C++ would make the included functions, classes, etc. available only to the scope of the including file.
Is there an issue with including a header in a main module and also in another header? Should I be using #ifndef, etc. directives in all my headers to avoid complications with this?

Yes, guard all your headers with #ifndef directives.

And your origional question seemed to be about whether or not you could include <string> multiples: well all the standard headers use #ifndef too! Cool, right?

Cool, right?

Wrong.

Muhahahahahahah! C++ is never cool.

For the sake of information, are you saying I am wrong about headers, or just spouting your opinion on C++?

For the sake of information, are you saying I am wrong about headers, or just spouting your opinion on C++?

Just for the sake of information, are you just refusing to recognize my authority on the coolness of things, or just feeling butthurt about some nonsensical post on the internet?

I was just wondering what your "wrong" comment was in response to. If you were saying that standard c++ headers in fact do not use #ifndef statements, that would be important. I see that is not the case.

in the source file..u can just go..

#include<string>
#include...
#include"someheader.h"
#include"anotherheader.h"

and remove #include<string> in the "someheader.h"

in the source file..u can just go..

#include<string>
#include...
#include"someheader.h"
#include"anotherheader.h"

and remove #include<string> in the "someheader.h"

If you had read any of the previous posts, you would have seen this point:

So, my question is: why would you want to require people to follow extra instructions just to use your header file?

If you had read any of the previous posts, you would have seen this point:

I don't have problems with that..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.