c++ - 如何防止clang自动包含头文件?

标签 c++ visual-c++ clang

我正在与一位使用 MSVC 的 friend 合作,我正在使用 clang。我注意到的一件事是 clang 似乎自动包含一些标准 header ,而 MSVC 则不是。例如,我只是分别使用了 assert.h 和 math.h 中的函数 assert() 和 round()。

代码在没有明确包含 header 的情况下符合我的要求——是否有一个开关可以关闭此行为?它把我逼上墙了。我希望出现错误,除非我明确包含 header 。

最佳答案

该标准允许标准头文件相互包含,或转发声明通常在其他文件中找到的项目...最有可能发生的是这些定义是由您 < em>是包括。

此行为符合标准,因此不幸的是,编写可移植代码将需要使用多个编译器进行测试。

A C++ header may include other C++ headers. A C++ header shall provide the declarations and definitions that appear in its synopsis. A C++ header shown in its synopsis as including other C++ headers shall provide the declarations and definitions that appear in the synopses of those other headers.

Certain types and macros are defined in more than one header. Every such entity shall be defined such that any header that defines it may be included after any other header that also defines it (3.2).

The C standard headers (D.5) shall include only their corresponding C++ standard header, as described in 17.6.1.2.

除了其他头文件包含的头文件之外,还有一个事实是 C++ 头文件可能会或可能不会引入全局名称,而 C 头文件可能会或可能不会在 namespace 内引入名称 std .我看到很多人推荐“这是 C++,你应该使用 <cxyz> 而不是 xyz.h”。这个建议具有误导性;这两个 header 不提供相同的定义,因此 C++ header 的包含不能简单地替换 C header 的包含。是否应该重写所有代码以改为使用 C++ 限定名称是值得商榷的……但这是更改包含的先决条件。


可能对您有帮助的一件事是使用 gcc -M 列出实际使用的 header .然后您可以决定是否应在您的代码中列出任何间接包含的 header 。

关于c++ - 如何防止clang自动包含头文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864276/

相关文章:

C++:x64 不推荐使用 A2W 宏 - 替代方案?

c++ - 是否可以在编译时检查是否包含枚举的语句

c++ - 与 "LPCWSTR"类型的参数不兼容

c++ - AfxMessageBox - 访问冲突

c - 如何使用命令行选项覆盖 __TIME__ 和 __DATE__ 宏的值?

c++ - Windows 上的 Qt 5.7 黑屏

C++ unix 32 位到在 x86_64 芯片(64 位服务器)上运行的 Red Hat Linux

c++ - 为什么 MSVC 选择 long long 作为 -2147483648 的类型?

macos - 使用 llvm/clang 链接 "toy"操作系统

c++ - clang,libtooling 添加新的规范内置数据类型