简体   繁体   English

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

[英]How to prevent clang from automatically including header files?

I'm collaborating with a friend using MSVC and I'm using clang. 我正在与使用MSVC的朋友合作,并且正在使用clang。 One thing I've noticed is that clang seems to be automatically including some standard headers, while MSVC is not. 我注意到的一件事是,clang似乎自动包含了一些标准标头,而MSVC没有。 For example, I just used the functions assert() and round() from assert.h and math.h respectively. 例如,我只使用了assert.h和math.h中的函数assert()和round()。

The code complied fine for me without explicitly including the headers -- is there a switch to turn this behavior off? 代码对我来说很好,没有显式包含标头-是否可以关闭此行为? It's driving me up a wall. 它把我推高了。 I want there to be an error unless I explicitly include the header. 我希望有一个错误,除非我明确包含标题。

The Standard allows standard header files to include each other, or forward declare items normally found in other files... most likely what is happening is that these definitions are being provided (directly or indirectly) by one of the headers you are including. Standard允许标准头文件互相包含,或转发通常在其他文件中发现的声明项……最有可能发生的是,这些定义是由您包含的头之一(直接或间接)提供的。

This behavior is Standard-conformant, so unfortunately writing portable code is going to require testing using multiple compilers. 此行为符合标准,因此不幸的是,编写可移植代码将需要使用多个编译器进行测试。

A C++ header may include other C++ headers. C ++标头可以包括其他C ++标头。 A C++ header shall provide the declarations and definitions that appear in its synopsis. C ++标头应提供在其提要中出现的声明和定义。 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. 提要中显示为包括其他C ++头的C ++头应提供出现在那些其他头的提要中的声明和定义。

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). 每个这样的实体应被定义为使得定义该实体的任何报头可以被包含在也定义该实体的任何其他报头之后(3.2)。

The C standard headers (D.5) shall include only their corresponding C++ standard header, as described in 17.6.1.2. C标准头(D.5)应仅包括其相应的C ++标准头,如17.6.1.2中所述。

In addition to headers included by other headers, there's also the fact that the C++ headers may or may not introduce global names, while the C headers may or may not introduce names inside namespace std . 除了其他标头包含的标头之外,还有一个事实是C ++标头可能会或可能不会引入全局名称,而C标头可能会或可能不会引入名称空间std名称。 I've seen a lot of people recommending "This is C++, you should be using <cxyz> instead of xyz.h ". 我见过很多人建议“这是C ++,您应该使用<cxyz>而不是xyz.h ”。 This advice is misleading ; 这个建议是误导的 the two headers do not provide the same definitions, so inclusions of the C++ header cannot simply replace inclusion of the C header. 这两个标头没有提供相同的定义,因此包含C ++标头不能简单地替换包含C标头。 Whether all the code should be rewritten to use the C++ qualified names instead is debatable... but is a prerequisite for changing the includes. 是否应重写所有代码以使用C ++限定名称,尚有待商...……但这是更改include的先决条件。


One thing that may help you is to list the headers actually used using gcc -M . 可能会帮助您的一件事是列出使用gcc -M实际使用的标头。 Then you can decide whether any of the indirectly included headers should be listed in your code. 然后,您可以决定是否在代码中列出任何间接包含的标头。

Standard headers can include each other. 标准标头可以互相包含。 The headers you want to not include might be included by another header you use. 您不希望包含的标题可能会包含在您使用的另一个标题中。

In this case you may want to manually #define the header guards of the headers you don't want before including anything, but that's a little dirty. 在这种情况下,您可能想要在包含任何内容之前手动#define不需要的标题的标题防护,但这有点脏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM