Reputation: 1697
I am working on a project using IAR.
Within the project, there are multiple locations to search for include files.
There are two locations which both have the same name file.
So, within the C file we have #include "some_file.h"
How does the compiler/preprocessor handle this? Does it include the first some_file.h
it finds?
My guess... it searches in the directories in the order that they are defined within the IAR project definition. It then stops at the first match. Is this correct?
Upvotes: 2
Views: 548
Reputation: 345
Usually when using #include "foo.h"
the file is searched relative to the current source file first, while #include <foo.h>
will prefer standard paths.
See GCC documentation:
GCC looks for headers requested with #include "file" first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first in /usr/include/sys, then in its usual search path.
Addition:
Also usually custom paths are searched in the order they appeared on the command line.
So for -I/opt/include -I$dependency/include
and #include "foo.h"
the following paths will be searched:
You can also find out which files were included exactly by looking at the output of gcc -E foo.c
.
See here for how -I
and -iquote
work.
Upvotes: 2
Reputation: 188
As far as I can tell it does search in the order that they are defined.
You could test it by changing or adding some value in one of the two headers to see which one is included or simply rename one.
Upvotes: 0