1

I am trying to learn different compilation tricks. Please consider following code snippet :

#include <header.h>
   main()
  {
     execute me;

  }

Now I am compiling this code using : -

gcc hello.c -I /home/example

what I am seeing during compilation of this file headers are being searched at /usr/include/ etc paths but I have placed header.h /home/example/header.h path so this is not able to find header file.

But if now I include header file in following manner then It is able to find header file.

#include "header.h"

So I am wondering if there is any way in which I will include header file using <> options and I also I able to give header path using command line (using -I or any option) ?

Please comment if something not clear.

Pradeep Goswami
  • 1,785
  • 1
  • 15
  • 24
  • 1
    If you have given a path using `-I`, gcc will search it no matter how you included it. How did you decide this?: "but I have placed header.h /home/example/header.h path so this is not able to find header file". Did you get an error saying it's not able to find? – P.P Dec 21 '15 at 14:00
  • And also see this post: http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename – terence hill Dec 21 '15 at 14:03
  • Does the behaviour vary if you put the `-I` before the source file? (`gcc -I /home/example hello.c`)? It doesn't seem to matter for me using GCC 5.3.0 on Mac OS X 10.11.2. But it does matter which order the `-I` directives are specified. What does adding the `-H` option tell you? – Jonathan Leffler Dec 21 '15 at 14:57
  • @terencehill you are right :) – Pradeep Goswami Dec 21 '15 at 15:08

2 Answers2

1

Including header files with these <> symbols actually tells the compiler to search it in the general directory and including with these "" symbols tells the compiler to search in the local project directory.

0

Are you sure it doesn't find the header? Because this provably does work. Did you perhaps misspell the directory following -I?

$ echo '#error here!' > header.h
$ cat test.c
#include <header.h>
int main(void)
{
     return 0;
}
$ gcc -I $PWD test.c
In file included from test.c:1:0:
/home/user/header.h:1:2: error: #error here!
 #error here!
  ^

Same result for #include "header".

Jens
  • 69,818
  • 15
  • 125
  • 179