Caesar
Caesar

Reputation: 9843

C++ #include - Only partial #include

Sometimes I run into situation where I find that I need to make an #include to large third party file just so I can use one function or a small class and that makes me feel guiltiy as I know that this is gone increase my compile time as it will complie the whole file when I just wanted that one function. Is there any way to get around this and only include that one function that I want without rewriting the whole thing?

Upvotes: 6

Views: 13709

Answers (4)

bames53
bames53

Reputation: 88175

Sometimes it may be reasonable to simply write your own forward declarations rather than include a header. Although if you're not in charge of defining the thing then you may run into problems where the definition changes and you have to update your forward declarations.

For example, some Boost libraries have attempted to forward declare some things in the standard library in order to avoid #includes. Recently they've run into problems with the libc++ implementation of the standard library because libc++ uses inline namespaces, so the naive forward declarations don't work with libc++. In fact a standard library implementation can do a lot of different things that conform to the standard but could break forward declarations that work for different implementations.

So, manual forward declarations are fragile, and possibly non-portable, but might work for you.

Another option is to use precompiled headers. This can cut down on the processing necessary to include a header. For example I have a scratch project I keep around where I include every header from the standard library. Without a precompiled header compiling an empty program takes a few seconds. Using a precompiled header an empty program takes a small fraction of a second.

You'll have to see how your compiler handles precompiled headers. Gcc and clang for example produce a precompiled header when you process a header file using -x c++-header or -x c-header. And then when you use the -include flag to include a file the compiler will search for a precompiled version of that header (using a compiler specific naming convention) and may include that instead.

$ cat everything.h

#include <iostream>

$ cat main.cpp

int main() {
    std::cout << "Hello, World!\n";
}

$ clang++ -x c++-header everything.h -o everything.h.pch

$ clang++ main.cpp -include everything.h

$ ./a.out

Hello, World

Clang looks for .pch while gcc looks for .gch

Upvotes: 2

Jay
Jay

Reputation: 14471

The header may not increase your compile time by a large factor. It depends on what's in it. It's certainly possible to edit out things you don't need but I don't recommend it. It's error prone and is usually a lot of effort for very little pay back. Does the header have include guards? If not, you might consider adding them if you're getting slow compilation.

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168646

You have some options:

  • Just include the header. Really, it won't increase your compile time by that much.
  • Complain to your 3rd-party header provider. Tell them to ship smaller, more modular headers.
  • Don't include the header, but declare as extern the single function you care about, like so extern int GetMeaningOfLife(int mice);

Upvotes: 12

Eran
Eran

Reputation: 22020

You can't compile only part of the header. If that 3rd party have their headers pull a lot of code, there's nothing you can do about it. The best solution I can think of is using precompiled headers, which should eliminate some of the compilation overhead, as those headers will only be fully compiled once. Another option is to use forward declaration of classes instead of including their declaration, if some other translation unit does include the header. This will only let you use those classes as pointers, though.

Upvotes: 4

Related Questions