Home > Mobile >  C / C includes & good practices?
C / C includes & good practices?

Time:01-21

I have seen multiple times people include headers files subfolders in two ways

  1. #include "header.h" and add path/to/subfolder in include paths.
  2. #include "path/to/subfolder/header.h" and add only root/folder in include paths.

Not sure if this is just a matter of choice or there are any good bad practice rules around it.

CodePudding user response:

An issue that can arise for case 1, but not case 2 is where you have two header files with the same name, but live in different directories, e.g. foo/utils.h and bar/utils.h. Using convention 2 from the outset eliminates this possibility.

CodePudding user response:

In general use paths relative to the including file (= option 2.), if you don't see a risk of having to move the files relative to each other and use paths relative to a directory passed as compiler option otherwise.

The benefit of a path relative to the including file is that tools can pick up the included files outside of the context of a project/in the absence knowlege about include directories. This could come handy, if you just want to take a quick look at a source file without opening the whole corresponding project the IDE.

You may want to distinguish between both alternatives by using #include <...> to refer to files searched relative to a path passed as compiler option btw, since it's not always immediately obvious where to look for a included file as a human without the help of tools which may not always be available.

  •  Tags:  
  • Related