Search code examples
c++header-filesbuild-definition

How to include only one symbol from a header file in C++


I have a big header file, but in a program, I only need a few specific variable defined in that header.

Is there any way in c++ that I tell it to include only that specific variable ?

And, yes, the variable has to be in that header file only.

What is the disadvantage if I include the entire header file (as I am doing it now) ?


Solution

  • You don't need to include the header for a single variable, just declare it locally:

    extern Type variable;
    

    There's no fundamental disadvantage to including a header file you need. It will increase compilation time, especially if you change the header and compile multiple times, but for clarity it should be included. Think of the case where you rename the variable, or change its type - you don't want to rename it in hundreds of places that use it (although some IDE's make this fairly easy), you want to have a central access point.