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.

4
  • 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? Commented Dec 21, 2015 at 14:00
  • And also see this post: stackoverflow.com/questions/21593/… Commented Dec 21, 2015 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? Commented Dec 21, 2015 at 14:57
  • @terencehill you are right :) Commented Dec 21, 2015 at 15:08

2 Answers 2

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.

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

Comments

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".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.