应该为我的项目使用相对包含路径,还是将include-directory放在包含路径上? [英] Should I use relative include paths for my project, or place the include-directory on the include path?

查看:300
本文介绍了应该为我的项目使用相对包含路径,还是将include-directory放在包含路径上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我目前使用相对路径来包含我的文件,这是不可改变的。

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.

例如,在我当前的项目中,我有 network / server / myfile.hpp 。它需要包括 common / log.hpp 。当前我使用 #include../../ common / log.hpp这是相当冗长,但工作。

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.

如果我在路径上添加我的主要include目录,我可以简单地包括common / log.hpp

If i instead add my main include directory on the path, I could simply include "common/log.hpp".

我知道这个问题可能更多地关于偏好比任何其他,但是有关于跨平台应用程序和什么C ++约定有什么客观的利弊?

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?

推荐答案

相对包含 .. 的路径看起来有点丑陋,并期望一个特定的文件系统结构, $ c>../../ common / log.hpp是两个文件夹。避免不必要的依赖关系,特别是文件系统结构,所以将头文件从一个目录移动到另一个目录不会强制更新所有包含该头文件的源文件。

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; } }

包括它很方便和直观:

#include "foo/bar/Baz.h"

这篇关于应该为我的项目使用相对包含路径,还是将include-directory放在包含路径上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆