锵返回系统不同的编译器的错误包括文件

问题描述:

main.cpp中:锵返回系统不同的编译器的错误包括文件

#include "test.h" 
void main() {narrowingConversion();} 

包括/ test.h:包括include文件夹作为一个系统时

void narrowingConversion() {int i = 1; char a[1] = {i};} 

锵编译上述代码successfuly文件夹:

clang++ -std=c++0x -isystem./include main.cpp 

但是当包含文件夹正常时,clang失败:

clang++ -std=c++0x -I./include main.cpp 

./include/test.h:1:54: error: non-constant-expression cannot be narrowed from type 'int' to 'char' in initializer list [-Wc++11-narrowing]

问:为什么铛表现不同的系统和非系统文件?

+0

@jens你确定这不是'c'吗?对于c警告/错误也可能会出现此问题。 – m7913d

+0

@ m7913d人们对于删除C或C++标签的问题非常反感,因为他们没有立即看到两者的相关性。我知道这是基于错误标记的合法实例,但我认为民众过度纠正太多。但是,在这种情况下,我可以看到他们的观点:尽管我很确定这对C来说是一样的,但没有Clang展示相同行为的例子,所写的问题仅仅是关于C++。 –

Clang默认禁止系统标题的警告。它似乎认为C++ 11缩小了一个非致命错误并在这种情况下抑制了对此的诊断。

the manual,看到这个当标题为#include d从目录被认为是一个系统中的一个(其中-Isystem指定),你需要启用这个选项:

-Wsystem-headers

Enable warnings from system headers.

此标志可能是从GCC继承而来的,它可以通过它was added in 2000。该rationale为是:

The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘ #warning ’ (see Diagnostics), are suppressed while GCC is processing a system header. Macros defined in a system header are immune to a few warnings wherever they are expanded. […]

m7913dfound铛文档,GCC没有不给尽可能多的博览会相当于部分:

More information can be found here: Controlling Diagnostics in System Headers

但核心的结果是一样的:

Warnings are suppressed when they occur in system headers.

而且,正如我们所看到的,Clang似乎认为C++ 11并不是一个硬性错误,而是要压制它在没有-Wsystem-headers的情况下为系统标题。

+1

不是一个坏的假设,但它看起来像一个_error_,不仅是一个警告 – Ctx

+0

我直到,否则证明,至少有一些错误也包含在该横幅下。例如,C++ 11缩小错误只需要一个模糊的“诊断”,而不是编译失败。也许这个因素就是这个。当然,您可以通过在原始案例中添加'-Wsystem-headers'并查看它的内容来测试。 –

+1

即使是系统文件,添加'-Wsystem-headers'确实会导致缩小错误。 – m7913d