C++ include with full path

valentin picture valentin · Mar 30, 2014 · Viewed 31.9k times · Source

I find #include "../app/thing.h" very ugly and I would like to be able to import from the main root of my project, like this:

#include <project/app/submodule/thing.h>

(I know <> is generally used for external but I find it very cleaner)

How can I do that, from anywhere in my project?

Answer

Jonathan Leffler picture Jonathan Leffler · Mar 30, 2014

You simply need to ensure that your build process sets an option to specify the starting point of the search.

For example, if your header is in:

/work/username/src/project/app/submodule/thing.h

then you need to include (assuming a POSIX-compliant compiler; AFAICR, even MSVC uses /I as the analogue of -I):

-I/work/username/src

as one of the compiler options. You can use that path from anywhere in your project since it is absolute. You just need a defined way for your build system to know what the setting should be, so that when it is moved to /home/someone/src/, you have only one setting to change.