In my project, I currently use relative paths to include my files, which admittedly doesn't change often. However, it yields pretty weird include patterns, because I usually nest my files in alot of folders.
For example, in my current project I have network/server/myfile.hpp
. It needs to include common/log.hpp
. Current I use #include "../../common/log.hpp"
which is pretty verbose, but works.
If i instead add my main include directory on the path, I could simply include "common/log.hpp"
.
I know this question might be more about preference than anything else, but is there any objective pros and cons concerning cross platform applications and what about C++ conventions?
Unlike absolute paths, relative paths contain information that is only relative to the current document within the same website which avoids the need to provide a full absolute path. In simple words, relative path refers to a path relative to the location of the current webpage.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).
Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.
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. Double dots are used for moving up in the hierarchy.
Relative includes paths with ..
in it look a bit ugly and expect a certain filesystem structure, that is, "../../common/log.hpp"
is two folders up. It makes sense to avoid unnecessary dependencies in general and on filesystem structure in particular, so that moving a header file from one directory to another does not force you to update all source files that include that header.
It is also elegant to have your includes correspond to namespaces and classes. If, for example, you have:
namespace foo { namespace bar { struct Baz; } }
It is convenient and intuitive to include it like:
#include "foo/bar/Baz.h"
By having #include <common/log.hpp>
in your source file and having path to common/log.hpp
in your project settings (compiler options) you are protecting your source code from changes in case common/log.hpp
moves to some other place so I would recommend this approach. Note using angle brackets in this case - compiler should search for the header in directories which paths are specified by the /I
compiler option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With