Include Path Directory

Viewed 34553

I'm somewhat confused on how to add header files to C++ projects. Often times when I attempt to use a #include "genericheader.h", it says that the file can not be found. However, the file generally exists, it's simply that the path is not written correctly So my question, by using the #include "genericheader.h", where does the compiler look for this file? Does it look in the current directory of the file that is trying to include it? Or is it dependent on things such as the IDE?

If I'm trying to include a header file, is it generally best practice to have it placed within the directory of the current file trying to include it?

Apologies for the noobish question. Thanks!

4 Answers

The order for searches has been explained very well, here I will explain how to tell the compiler to search under certain directories. Suppose you are using bash, then in ~/.bashrc, write

C_INCLUDE_PATH="/full/path/to/your/file/:$C_INCLUDE_PATH" ## for C compiler
CPLUS_INCLUDE_PATH="/full/path/to/your/file/:$CPLUS_INCLUDE_PATH" ## for Cpp compiler
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH

and source it with source ~/.bashrc. You should be good to go.


One thing to be notice is that, if you have

#include "abc/xxx.h"

int main()
{
    return 0;
}

then your full path should not have abc.

Related