I have done some searching and found similar threads on the proper way to include header files in C++, but none of them quite answered this question.
I know that in order to include a header file in another folder you can simply use the following syntax:
#include "../myFolder/myFile.h"
But what about when the file is in a different directory structure a bit far removed? For example, if something like the following is guaranteed to be true:
Current directory = / f1 / f2 / f3 / f4 / f5 / myFile.cpp
Desired header directory = / f1 / d2 / d3 / d4 / d5 / d6 / myHeader.h
I know that you can set the "Additional Include Directories" property or use a make file, but I'd like to know if there is a way to do it from the #include
statement.
putting ".."
in #include
is generally considered ugly and unmaintainable.
Every coherent library you're using (for instance boost) has a single root of header files hierarchy that you should put in your "additional include directories" property. For boost it is something like
`C:/lib/boost_1_49`
under this directory you usually find a directory called boost
where all of the headers live. This brings the convention that all boost headers start with:
#include <boost/bla/bla.hpp>
This also goes for the very project you're writing. You should decide what's the best root for its headers and start all the includes from there.
The only exception to this rule should be headers that are in the same directory. Those can just be included as just a file name #include "same-dir-header.h"
You should also make the difference between including with ""
and <>
. Quotes should be things in your project, angle brackets should be external libraries (or as some would have it - OS and C runtime libraries)
To complete the answer from @shoosh, you are supposed to tell your compiler where are those "other" header files. With gcc on windows, if they are in c:\path\to\library, then add the -I option
-Ic:\path\to\library
Beware of spaces in path, if the location is c:\my path\to\library
, then:
-I"c:\my path\to\library"
Other compilers will provide a similar option, on command line or through the IDE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With