1

QUESTION:

Is there a way to tell the compiler to look the headers files in a specific directory ? I would like to use

#include "my_file.h"

Instead of

#include "/path/to/mylib/include/my_file.h"

(I'm trying to see how I could organize a custom library where headers files would not be in the same folder as the programs).

Sorry for the previous answerers, I had to completely recreate my question as it was not clear.

ANSWER:

Finally I found the answer. I could not add an answer since my post has been marked as duplicate (I believe), but anyway, I could use any of the following:

#include <my_file.h>
#include "my_file.h"

The "trick" was to use the -I option, which adds a header directory.

gcc -I/path/to/mylib/include myprogram.c ~/path/to/mylib/src/file/my_file.c -o myprogram && ./myprogram

In this case, the structure is:

  • /path/to/mylib/include/my_file.h
  • /path/to/mylib/src/my_file.c
  • /path/to/myprogs/myprogram.c

And the current dir was /path/to/myprogs

ling
  • 9,545
  • 4
  • 52
  • 49
  • I don't want to post a full answer because I'm not 100% sure, but I believe you need to use double quotes instead of angled brackets (greater than and less than signs). Angled brackets search the system header directory whereas quotes search your project directory and then search the system directory if it cannot find the file. This is compiler specific though, so I cannot say that is the issue with 100% confidence. Also, you should not need `-I/path/to/include` if that include path is simply for your own header file. – Spencer D Sep 21 '14 at 17:38

2 Answers2

2

For your own headers you are supposed to use #include "file" - the search paths are different if you use #include <file> since that's supposed to be used for system includes.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Yes I figured that out, but maybe my question is not correctly asked. My problem is that I would have to use a full path, like "/path/to/file.h" instead of just "file.h", because I'm trying to make a library and plan to reuse it in other projects, so I'm looking for a way I could include "file.h", without to specify the whole path in the file. I should probably update my question. – ling Sep 21 '14 at 17:45
0

Use this:

#include "my_file.h"

To know exact reason why please see following link

Community
  • 1
  • 1
Hemant Gangwar
  • 2,172
  • 15
  • 27