gpt4 book ai didi

c++ - 包括头文件样式 - C++

转载 作者:可可西里 更新时间:2023-11-01 17:38:41 25 4
gpt4 key购买 nike

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

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

所以 src/module1 中的 foo.cpp 文件必须包含 like,

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

这看起来很乱而且很难写。我发现写作包括像

#include <module1/foo.h>

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

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

最佳答案

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

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

更好地了解使用哪一个(包括是使用引号还是尖括号)可以从标准中得到。

来 self 的 C++ 草稿拷贝:

16.2 Source file inclusion

2 A preprocessing directive of the form

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

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

3 A preprocessing directive of the form

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

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

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

with the identical contained sequence (including > characters, if any) from the original directive.

7 Although an implementation may provide a mechanism for making arbitrary source files available to the < > search, in general programmers should use the < > form for headers provided with the implementation, and the " " form for sources outside the control of the implementation.

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com