Ordering of Include Statements to Reduce Hidden Dependencies

23 July, 2009 § 2 Comments

The Google C++ Style Guide says that #include statements should be ordered in the following way (if you are writing foo2.cc):

  1. dir2/foo2.h (preferred location — see details below).
  2. C system files.
  3. C++ system files.
  4. Other libraries’ .h files.
  5. Your project’s.h files.

The goal of this is to reduce hidden dependencies within header files. My question is, shouldn’t system files be included after library/project files? I would think that there is a guarantee that the system files aren’t going to include other library/project files, so wouldn’t the following order make more sense to find hidden dependencies?

  1. dir2/foo2.h (preferred location — see details below).
  2. Your project’s.h files.
  3. Other libraries’ .h files.
  4. C system files.
  5. C++ system files.

Tagged: , ,

§ 2 Responses to Ordering of Include Statements to Reduce Hidden Dependencies

  • A.J. says:

    You bring up a good point Jared!

    For instance you could have in your code
    //Lots of stuff above; and near the bottom of a .cpp/.h file
    #include <Something.h>
    void function() { /*statements*/ }

    With what you have noted with the Google C++ Style Guide. Whereas if only system files were below project’s .h files you still could #include near the bottom of a .cpp/.h file only you’d be including something which would NOT include other library/project files.

    But still I think it is about the ordering of the includes and not about the order in which the compiler includes the files.
    So say you have a file called stdio.h that looks like the following:
    #include <iostream>
    void printf2(const char*, int) { std::cout << "My printf2"; }

    You then could have a main.cpp like the following:
    #include "stdio.h" //"" means use project's files before system's files
    #include <stdio.h> //<> means use system's files before project's files

    int main()
    {
    printf2("", 0);
    return 0;
    }

    It doesn't have to do with hidden dependencies but rather about the order in which the compilier looks for the included file. And so if you want the compilier to include files in the order of:
    1. dir2/foo2.h / Your project’s.h files.
    2. Other libraries’ .h files / C system files / C++ system files.

    Then I think just use double quotes for all includes. Compiling will be a little bit longer though. Plus you could make interesting source🙂

    Yet another, imagine you have the following cout.h file:
    void function() { std::cout <= 0x0501
    //Statement(s)
    #endif

    I am assuming WINVER got defined in one of my include files. If not substitute WINVER with someother define. I like making my source code behave differently depending on what is defined.

  • A.J. says:

    You bring up a good point Jared!

    Often times my project files depend on something from an include file. For instance:
    #if WINVER >= 0x0501
    //Statement(s)
    #endif

    I am assuming WINVER got defined in one of my include files. If not substitute WINVER with someother define. I like making my source code behave differently depending on what is defined/what it can do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Ordering of Include Statements to Reduce Hidden Dependencies at JAWS.

meta