0

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

I would like to set an include path for my mysql.h file which is located in another location, such as: "root/a/b/c".

I've tried:

#include "/a/b/c/mysql.h"

But it doesn't compile. Am I missing an option?

Community
  • 1
  • 1
paul smith
  • 25
  • 1
  • 1
  • 3
  • compiler is c. I do " cc test.c "" – paul smith Jun 28 '12 at 02:04
  • c file resides in somewhere like " / d/e/f/g " – paul smith Jun 28 '12 at 02:05
  • so, root is the common super directory with mysql.h file . – paul smith Jun 28 '12 at 02:05
  • 2
    `cc` most likely indicates you're using the GNU compiler. Try `#include `, and compile with `cc -o test test.c -I"/a/b/c"`. – jweyrich Jun 28 '12 at 02:06
  • It's been my experience that if your .h file is in a different directory you need to put angle brackets (<>) around the base name of the .h file (e.g. ) and then tell the compiler where to find the include file. As another comment implied, how to do this will depend on your compiler. – GreenMatt Jun 28 '12 at 02:06

1 Answers1

6

In your .c file, you can use relative path to include the .h file. For example, if .c file is in /d/e/f/g folder and .h file is in /a/b/c folder, then your #include statement has to be

#include "../../../../a/b/c/mysql.h"

This is acceptable if the number of slashes is around 1 or 2 (2 is a bit dirty already). And it is not good if you move the folders around later.

Alternatively, you can only specify the name of the .h file, and use -I <dir> flag where you replace <dir> with the path to the folder that has .h file. If you move the folders around, you can just change a bit in the the compilation command (usually in a makefile).

nhahtdh
  • 55,989
  • 15
  • 126
  • 162