What are the benefits of a relative path such as "../include/header.h" for a header?

c include path environment variable
relative path c
c++ relative path include
c programming relative path
c++ create directory relative path
c++ include path
header path
gcc path

I've reviewed questions How to use include directive correctly and C++ #include semantics and neither addresses this - nor do the others suggested by SO when I typed the title...

What, if any, are the benefits of writing:

#include "../include/someheader.h"
#include "../otherdir/another.h"

compared with using just a plain file name:

#include "someheader.h"
#include "another.h"

or perhaps a relative name without the '..':

#include "include/someheader.h"
#include "otherdir/another.h"

The problems I see are:

  • You can't move a header without worrying about which source files include it.
  • You can end up with very long paths for headers in dependencies and error reports. I had one today with "../dir1/include/../../include/../dir2/../include/header.h".

The only merit I can see is that while you do not need to move files around, you might be able to get away without always using '-I' directives to find headers, but the loss of flexibility, and the complexity of compiling in sub-sub-directories, etc seems to outweigh the benefit.

So, am I overlooking a benefit?

Thanks for the inputs. I think the consensus is that there aren't any major benefits to the notation using ".." that I'm overlooking. In general terms, I like the "somewhere/header.h" notation; I do use it in new projects. The one I'm working on is anything but new.

One of the problems is that there are various sets of headers, often with a prefix such as rspqr.h, rsabc.h, rsdef.h, rsxyz.h. These are all related to code in the rsmp directory, but some of the headers are in rsmp and others are in the central include directory, which does not have sub-directories such as rsmp in it. (And repeat for the various other areas of the code; there are headers in multiple locations, needed randomly by other bits of code.) Moving stuff around is a major problem because the code has gotten so convoluted over the years. And the makefiles are not consistent in which -I options are provided. All in all, it is a sad story of not-so-benign neglect over a period of decades. Fixing it all without breaking anything is going to be a long, tedious job.

I prefer the path syntax as it makes it very clear what namespace or module the header file belongs to.

#include "Physics/Solver.h"

Is very self-describing without requiring every module to prefix their name to header files.

I almost never use the ".." syntax though, instead I have my project includes specify the correct base locations.

What are the benefits of a relative path such as "../include/header.h , Relative path vs absolute path: What do they mean?# For example, imagine this same root directory also has a folder However, using absolute URLs does have some SEO advantages, which will be discussed later on. Therefore, the link's path is relative to the current document being displayed by the browser, hence the term relative path. You would need to include a protocol and your domain name within the link to turn the same example as above into an absolute path link:

Absolute vs Relative Path - Which Should You Be Using?, paths more secure? For example if I would move my internal html file accidentally into another folder, the relative path would be incorrect and thus the l​… An absolute, or full, path begins with a drive letter followed by a colon, such as D:. Relative path. A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory.

IANALL, but I don't think you should be putting ..'s in actual C or C++ source files, because that's not portable and the standard does not support it. This is similar to using \'s on Windows. Only do it if your compiler can't work with any other method.

What are the advantages of using relative paths?, For example, it will be able to work on localhost, as well as on your public Relative paths as explained by the rest has the benefit of porting  Relative Paths. A relative path is a way to specify the location of a directory relative to another directory. For example, suppose your documents are in C:\Sample\Documents and your index is in C:\Sample\Index. The absolute path for the documents would be C:\Sample\Documents.

What is the benefit of building a site with relative paths?, They are (as the post tile implies) Absolute, Relative, or Root Relative. The advantage of using absolute file paths is that they are much easier to used on multiple websites (for example a wordpress plugin, or a Javascript  Alternatively referred to as a partial path or non-absolute path, a relative path is a URL that only contains a portion of the full path. Its complete location is based on its relation to the directory to which it is linking.

Absolute File Paths VS. Relative File Paths VS , There are advantages and disadvantages to both. For example, if we were to take a relative path from the previous example, it could look like  A relative path is a path that describes the location of a file or folder in relative to the current working directory. It can be best used to refer to websites that are located on the same domain, ideally on certain sections of websites in which the documents never change relationships to each other.

Choosing between relative and absolute paths for hyperlinks, For example, when a hyperlink is configured to open another PDF, Revu folder (Full Path), or relative to the location of whatever is accessing it (Relative Path). Relative URLs will also cause your page to load slightly faster. However, in my experience, the SEO benefits of having absolute versus relative URLs in your website far outweigh the teeny-tiny bit longer that it will take the page to load. It's very negligible. If you have a really, really long page load time,

Understanding Relative vs Full Paths, A relative path cannot span disk drives. For example, if your current directory is on drive D, you cannot use relative paths to navigate to any directory on the E drive  Windows non absolute path (relative path) calc.exe In the above example, the absolute path contains the full path to the file and not only the file name as seen in the non absolute path .

Paths explained: Absolute, relative, UNC, and URL—Help, The difference between relative and absolute file paths and how to figure them and their main advantage is the fact that the file will load faster than one in the The file name must NOT contain any spaces or special characters such as an  In such cases you will receive additional Holiday pay. PENSION PLAN To be eligible you must work at least 20 hours. Eligible employees are enrolled in a 403(b) pension plan. PATH contributes 3% of your annual salary at no cost to you. In addition, PATH will match 25% for every dollar that you contribute to the plan up to 10% of your base salary.

  • You seem to be aware of all the factors involved so this isn't really a question.
  • I am checking whether there are benefits I've overlooked (because I hate the notation but suffer from it at work). I have less problem with the notation without the ".." in it.
  • This (using relative paths) tends to lead to enormous headaches when using a separate build dir. I.e cd yourproject; mkdir build; cd build; ../configure && make - If you are going to keep maintaining the code, it may be worth the time to fix that.
  • Believe me, tinkertim, I am working on it. However, I do not have permission to make the change uniformly; it takes time to make this change (years - the guardians of the code base are horribly conservative).
  • yeah that thing using "projectname/include/header.h" was what i used in my last project (but started from include/ instead). i always put only one -I that points to the include/ path and then i include based on that. but i didn't analyze the ".." problems in depth yet. You have good points.
  • This seems to describe ways in which header paths are brittle in general, rather than relative paths in particular. One could easily come up with equivalent problem scenarios which don't involve ...
  • "I wouldn't add any include path entries other than -I...." It's far more common to add the 'include' directory, i.e. -Iinclude. Then your problems go away and .. can be used in cases where it's appropriate. Having lib and include in your include paths this way seems like a code smell.
  • Good point. The standard doesn't even seem to guarantee that you can use forward-slashes. "The implementation provides unique mappings for sequences consisting of one or more nondigits (lex.name) followed by a period (.) and a single nondigit." It defines nondigit as basically [_a-zA-Z] or \uNNNN.
  • This is true - but the software in question has been compiling on a wide range of platforms for a number of years (more than twenty of them), so it is more a theoretical than a practical liability.
  • Zero google hits for IANALL. I assume it's "I am not a language lawyer"?
  • In a Neuronal and Lexical Lingo?
  • @singlenegationelimination please provide some evidence that the standard does not support ..'s.