1

Surprisingly, the only really similar question I found (Advantages of removing includes which are already in precompiled header?) only asks for possible compile time gains, and mentions there are disadvantages but without specifying what these might be. There are other related questions but these concern including headers in headers and not directly in translation units, so I wanted to ask specifically about this.

Consider:

precompile.hpp

#ifndef PRECOMPILE_H
#define PRECOMPILE_H

#include <string>
#include <iostream>

#endif // PRECOMPILE_H

precompile.cpp

#include "precompile.hpp"

main.cpp

#include "precompile.hpp"

#include <string>
#include <iostream>

int main()
{
    std::string str = "hello";
    std::cout << str << "\n";
}

Are there any major recommendations as to whether or not to explicitly include, in the source file, headers that are precompiled (<string> and <iostream> in my example)?

Marc.2377
  • 7,807
  • 7
  • 51
  • 95

1 Answers1

1

You could argue that any source file should #include everything it depends on, regardless of what's precompiled.

Such an approach makes it easier to reuse code in other projects, that may not have those dependencies precompiled.

Sid S
  • 6,037
  • 2
  • 18
  • 24