Should I use relative include paths for my project, or place the include-directory on the include path?(我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上?)
问题描述
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?
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"
这篇关于我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上?
基础教程推荐
- C++实战之二进制数据处理与封装 2023-05-29
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- 全面了解C语言 static 关键字 2023-03-26
- [c语言-函数]不定量参数 2023-09-08
- C语言 详解字符串基础 2023-03-27
- C++实现ETW进行进程变动监控详解 2023-05-15
- 带你深度走入C语言取整以及4种函数 2022-09-17
- [C语言]二叉搜索树 2023-09-07
- centos 7 vscode cmake 编译c++工程 2023-09-17
- C语言实现宾馆管理系统课程设计 2023-03-13
