Is the #include order guaranteed by the standard to be the order of the -I options in the invocation:


having the folders and files:


can it be guaranteed that

#include <foo.h>

when cpp is invoqued with

... -I A -I B ...

will choose A/foo.h ?

  • 1
    Note SO is a more appropriate site for this question, for even though it asks about the standard, answers will be at the tool level.
    – outis
    Sep 26, 2016 at 9:49

1 Answer 1


No, because an implementation doesn't have to take command line options. § 16.2.2 states for the angle form (or system header inclusion; there isn't a standard name I'm aware of (sound off in the comments)), the preprocessor:

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters

The header doesn't even need to be a file.

Specific compilers may make this guarantee. The g++ manual, for example, states:

If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

  • Thanks. In my particular case I'm using two compilers (one of them g++) and both behave this way. I just wanted to know if this is standard or just by chance. Sep 26, 2016 at 9:48
  • @ToniHomedesiSaun: Not really just by chance, but a quality of implementation issue that the standard doesn't try to address. Sep 27, 2016 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.