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

I reviewed the issues of using the include directive and C ++ #include semantics correctly , and none of the addresses are the others suggested by SO when I typed in the name ...

What, if any, are the advantages of writing:

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

compared to using a simple file name:

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

or possibly a relative name without ' .. ':

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

I see the following problems:

  • You cannot move the header without worrying about which source files include it.
  • You can get very long paths for headers in dependencies and bug reports. Today I had " ../dir1/include/../../include/../dir2/../include/header.h ".

The only thing I see is that although you do not need to move files, you can leave, not always using the โ€œ -I โ€ directives to search for headers, but the loss of flexibility and the complexity of compilation in sub-directories, etc., seems to outweigh the benefits.

So, I do not see the benefits?




Thanks for the input. I think the consensus is that there are no significant benefits to the notation using ".." which I skip. In general, I like the notation "somewhere / header.h"; I use it in new projects. The one I'm working on is nothing short of new.

One of the problems is that there are different groups of headers, often with a prefix, such as rspqr.h , rsabc.h , rsdef.h , rsxyz.h . They are all related to the code in the rsmp directory, but some headers are in rsmp , while others are in the central include directory, which does not have subdirectories such as rsmp . (And repeat for various other areas of the code, there are headers in several places that need to be randomly executed using other bits of code.) Moving material around is a serious problem because the code has become so confusing over the years. And make files are incompatible, which have -I options. All in all, this is a sad story of not so benevolent neglect for several decades. Fixing all this without breaking anything will be a long, tedious job.

+41
c include header
Feb 28 '09 at 1:36
source share
6 answers

I prefer the path syntax, as it very clearly defines which namespace or module belongs to the header file.

 #include "Physics/Solver.h" 

Very self-describing, without requiring each module to prefix their name with header files.

I almost never use the ".." syntax, although instead I have the correct base locations in my project.

+28
Feb 28 '09 at 1:42
source share

The problem with #include "../include/header.h" is that it often works by accident, and then, it would seem, a change that is not related to it will cause it to stop working later.

For example, consider the following source layout:

 ./include/header.h ./lib/library.c ./lib/feature/feature.c 

And let's say that you use the compiler with include -I. -I./lib -I. -I./lib . What's happening?

  • ./lib/library.c can do #include "../include/header.h" , which makes sense.
  • ./lib/feature/feature.c can also perform #include "../include/header.h" , although this does not make sense. This is because the compiler will try to set the #include directive relative to the location of the current file, and if that fails, it will try to set the #include directive for each -I entry in the #include path.

Also, if you later remove -I./lib from the #include path, you will break ./lib/feature/feature.c .

I find the following preferred:

 ./projectname/include/header.h ./projectname/lib/library.c ./projectname/lib/feature/feature.c 

I would not add any added path entries except -I. and then library.c and feature.c would use #include "projectname/include/header.h" . Assuming that the โ€œproject nameโ€ is likely to be unique, this should not lead to name clashes or ambiguities in most cases. You can also use the include path and / or make the VPATH function to split the physical layout of the project into several directories if it is absolutely necessary (for example, to place code specially created for a particular platform - this is what really breaks when you use #include "../../somefile.h" ).

+21
Feb 28 '09 at 3:17
source share

IANALL, but I donโ€™t think you should place .. in real C or C ++ source files, because it is not portable, and the standard does not support it. This is similar to using \ on Windows. Do this only if your compiler cannot work in any other way.

+8
Feb 28 '09 at 3:02
source share

Think of your source tree as a nested namespace, and the include path is to bring directories to the root of this namespace. Then the question arises of creating a logical namespace for your code base, regardless of how the code is organized on disk.

I would avoid paths like:

  • "include/foo/bar.h" - "include" seems illogical and redundant
  • "../foo/bar.h" - ".." suggests relative location and fragile.
  • "bar.h" - if bar.h is not in the current directory, it pollutes the global namespace and requests ambiguity.

Personally, I tend to add the following path to my projects: path - "..;../..;../../..;../../../.." .

This allows you to apply some kind of hiding rule to your #include and allow some freedom to move the headers without breaking any other code. Of course, this is due to the introduction of the risk of binding to the wrong header file, if you are not careful, as incompletely qualified names can (or become over time) ambiguous.

I tend to fully qualify #include in public headlines, so third parties buying my code do not need to add "..;../..;../../..;../../../.." to their project is just a convenience for my personal code and build system.

+2
Feb 28 '09 at 2:42
source share

Because then you put the file relative to the root of the project, and when you check it on the original control, and another developer checks it elsewhere on your local system, it still works.

+1
Feb 28 '09 at 1:41
source share

Another problem with windows with relative paths is MAX_PATH. This will cause compilation problems when, for example, cross-compiling for android, and your path increases to more than 260.

+1
Oct 13 '17 at 15:20
source share



All Articles