0

With gcc11 (11.3.1) I'm seeing a strange behavior where it seems that -idirafter path takes precedence over -iquote path if path is the same for both options. Consider:

// test/foo.hpp
#pragma once
auto foo{ char{ 256 } };
// test/foo.cpp
#include "test/foo.hpp"

As one would expect, if we try to compile form the parent directory of test/, we get

$ g++ -iquote. -c test/foo.cpp -o foo.o 
In file included from test/foo.cpp:1:
./test/foo.hpp:5:21: error: narrowing conversion of '256' from 'int' to 'char' [-Wnarrowing]
    5 | auto foo{ char{ 256 } };
      |

However, adding -idirafter. makes everything compile:

$ g++ -iquote. -idirafter. -c test/foo.cpp -o foo.o

clang, gcc14 and gcc15 do not exhibit this behavior. However, I could not find any references to a bug in gcc11. I was wondering if anyone seen similar issue or there is a bug report I might've missed and if there is a possible patch? Obviously, if someone could confirm or dispel the issue, it'd be great.

Thanks!

1 Answer 1

2

This is not a bug.

GCC 3.17 Options for Directory Search

The -isystem and -idirafter options also mark the directory as a system directory, so that it gets the same special treatment that is applied to the standard system directories.

GCC 2.8 System Headers

GCC gives code found in system headers special treatment. ... All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header. ...

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I'm confused. Shouldn't -idirafter paths be the last ones to be searched? And, therefore, warnings should not be suppressed in case there is a valid -iquote path? From gcc.gnu.org/onlinedocs/gcc/Directory-Options.html Not to mention, gcc14, gcc15 and clang all issue warnings with or without -idirafter
No. These command line parameters are appending and not replacing. Once a path is listed as system, it's not a regular search path anymore.
Right, they are not replacing each other, they are all present. For a given quoted include: 1. the directory with the file containing the include directive is searched and if header is not found continue, otherwise terminate search. 2. iquote paths are searched and of header is found, the search is terminated. 3,4,5,6 are not searched in this case. gcc11 seems to override iquote with idirafter if their paths are the same, which seemingly incorrect behavior contradicting gcc.gnu.org/onlinedocs/gcc/Directory-Options.html precedence, and differs from gcc14, gcc14 and clang behavi

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.