我有一个具有以下目录结构的项目。

root
--include
----module1
----module2
--src
----module1
----module2

因此,在foo.cpp中说src/module1的文件必须包含like,
#include "../../include/module1/foo.hpp"

这看起来很困惑而且很难编写。我发现写作包括
#include <module1/foo.h>

并在编译看起来很整洁时提供root/include的包含文件搜索路径。但是,我不确定这种风格是否有缺点。

您更喜欢哪一个?为什么?您还发现以上述方式组织文件时是否有任何问题?

最佳答案

#include "../../include/module1/foo.hpp"

应尽可能避免指定路径。编译器为您提供了一种更清洁的替代方法来实现这一目标。此外,一个干净的设计应该确保您不需要弄乱包含 header 的相对路径。

最好使用哪个(包括使用引号还是尖括号)
可以从标准中得到。

从我的C++草案拷贝中:


#include <h-char-sequence> new-line`


# include "q-char-sequence" new-line


#include <h-char-sequence> new-line`

关于c++ - 包括头文件样式-C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2156349/

10-11 22:37
查看更多