Suppose we have the following program main.c:

#include <stdio.h>
#include "main.h"

int main()
{
extern int k;
printf("%d\n",k);
return 0;
}


And we compile it using

gcc -o main -I dir1 -I dir2 main.c


Then if there is a main.h in both dir1 and dir2, the one in dir1 will be used since it comes earlier in the command. If instead we use

gcc -o main -I dir2 -I dir1 main.c


to compile, then the one in dir2 will be used.

If there exists headers with the same name in the include directories,you can always explicitly set the header to use by using absolute path in the source file such as

#include "/home/foo/dir1/main.h"