0

Possible Duplicate:
How do I add a directory to C header include path?

I have run into this problem several times, and I thought it would be good to get an explanation from an experienced C or C++ programmer. In the example below I use a sample library name, but the actual name could be anything.

Say I want to run the following MWE:

#include <stdio.h>
#include <mylib/mylib.h> /* Also try mylib2/mylib/mylib.h */

int main (int argc, char **argv) {
    printf ("Hello, World!\n");
    return 0;
}

Further, assume that I have a Linux distribution that, instead of placing mylib.h in /usr/include/mylib/, chooses the directory /usr/include/mylib2/mylib/. So the straightforward command:

$ gcc test.c

would fail with the error:

fatal error: mylib.h: No such file or directory

But I might try to fix the include statement by writing:

#include <mylib2/mylib.h>

This gets me past the first error. But internally, mylib.h refers to other header files in the "usual" way: #include <mylib/anotherheader.h>. So now, even after I have fixed the original include statement in the MWE as #include <mylib2/mylib/mylib.h> the build breaks because mylib.h is attempting to include #include <mylib/anotherheader.h> instead of #include <mylib2/mylib/anotherheader.h>.

What is the standard way around this problem? There must be a simple compilation flag that I am missing. Thank you in advance.

Community
  • 1
  • 1
Shredderroy
  • 2,860
  • 2
  • 29
  • 53

3 Answers3

3

You set up the include path so that it contains /usr/include/mylib2, usually with a command line option to the compiler like -I/usr/include/mylib2.

Note that usually an header includes related files from the same package using the

#include "another.h"

syntax, so that it is first searched in a relative to where it is installed. If your library had done this, there would be no problem with

#include <mylib2/mylib/mylib.h>

but even if it was the case you don't want your code to depend on where the libraries you use are installed. That dependence should be kept in the build system.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
1

I would recommend the following pattern:

/usr/libs/lib15 - is the directory where your library resides.

In the code:

#include <stdio.h>
#include <mylib1.h>            // Header from your lib.
#include <mylib2.h>            // Other header from your lib.
#include <mygraphs/defines.h>  // Headers in your lib have even subdirectories.

In the command line:

cl -I /usr/libs/lib15 .....

You specify the search path for your headers.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
1

See the -I compiler flag on gcc and g++ it allows you to specify additional include paths:

http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

In this case you could specify: -I /usr/include/mylib2

and #include "mylib/mylib.h" everywhere

Also, this is not part of your question but I thought I would throw it in there anyways, but there is a slight difference between include statements with quotes, and angle brackets: http://msdn.microsoft.com/en-us/library/36k2cdd4(v=vs.80).aspx

Chris Desjardins
  • 2,631
  • 1
  • 23
  • 25