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