17

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?

valentin
  • 2,596
  • 6
  • 28
  • 48
  • 2
    See [What are the benefits of a relative path such as `"../include/header.h"` for a header?](http://stackoverflow.com/questions/597318/) — TL;DR, there aren't many benefits and there are liabilities. – Jonathan Leffler Mar 30 '14 at 01:00
  • 3
    Using a relative path for files which are always moved around together is just common sense. And for those, you must use "". – Deduplicator Mar 30 '14 at 01:06
  • 1
    Well, both of you convinced me, I'll go with relative path... – valentin Mar 30 '14 at 01:21

3 Answers3

9

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • It's not necessarily `-I` for each and every compiler. – πάντα ῥεῖ Mar 30 '14 at 01:04
  • Specify a compiler off Windows where it is not `-I`? I've added a caveat to the answer — and mentioned that I believe Windows uses `/I` since options there start with `/` instead of `-`. – Jonathan Leffler Mar 30 '14 at 01:06
  • I have not yet met a compiler that doesn't use `-I`, but of course, there are possibly compilers that doesn't. Even MS compilers take -I (even if they "prefer" /I) – Mats Petersson Mar 30 '14 at 01:06
  • Was windows mentioned anywhere in the question. There are some exotic toolchains that use different options for this feature. – πάντα ῥεῖ Mar 30 '14 at 01:08
  • @πάνταῥεῖ — OK; and you're not going to give any example of such an exotic toolchain, so we'll never know what you're thinking of unless we happen to run into it? At one level, I'm not dreadfully worried; I've covered the vast majority of practical cases. OTOH, I like to collect esoterica like knowledge of systems where the compiler options are all whacko by comparison with the normal ones. – Jonathan Leffler Mar 30 '14 at 01:11
  • Sorry it was meant as a side note not as major critisism, I just vaguely remembered that. I think it was some exotic video processing chipset. Their C/C++ compiler support was pretty odd and buggy stuff anyways :-/ ... – πάντα ῥεῖ Mar 30 '14 at 01:16
  • @JonathanLeffler, I'm sorry to reanimate this post. But I have a side question that could maybe help you improve your answer (or help me gather more info before asking a new one). There are codebases where a directive `#include ` actually includes the file `a/b/some/more/dirs/c/d.h`. Does this actually relate to the OP? – Enlico Jan 15 '20 at 10:37
1

You can simply use the include directories option of your current compiler (-I usually) to achieve this.

Also note using the "" double quotes will just add to fallback for the compiler standard headers. Files included using the <>, are only guaranteed to search files from the compiler standard headers.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

See this answer for a more complete explanation about how the differences between the two formats work. Honestly, though, I think you might want to consider restructuring your folder hierarchy if you need to jump up a folder then jump into another folder to get something. Generally speaking, it's pretty common practice to keep all files local to your program local to each other in the folder structure (i.e. in the same folder), and all files that aren't local, but may be needed (such as header files for libraries used) in a sub-folder within the main program folder, or to include them at compile time.

It is important to note that in the answer I linked above, it explains that "<>" includes are IMPLEMENTATION DEPENDENT, so we'd really need to know what compiler you're using to tell you if you could or couldn't do that.

Community
  • 1
  • 1
Gurgadurgen
  • 408
  • 3
  • 13