为什么Mac上的铿锵声自动包含一些缺失的标题 [英] Why clang on Mac automatically includes some missing headers?
问题描述
我注意到> clang ++ 在Mac上包含缺少的标头 - < limits>
,而 g ++ 显示错误关于它在Linux上。现在我想知道为什么 clang 这样做,而 gcc 不是。我可以如何强制 clang 不这样做。
以下是一个由 clang 编译的示例代码Mac上,而不是Linux上的 gcc :
#include< iostream>
使用namespace std;
int main()
{
cout<< int max:<< numeric_limits< int> :: max()<< ENDL;
}
UPD 我查看了库,这里是我发现的。 内部 g ++ 使用 libstdc ++ 标准库,它使用 使用过的编译器: I noticed that clang++ includes a missing header - Here is a sample code which compiles by clang on Mac, but not by gcc on Linux: UPD I looked into libraries and here is what I found. Internally clang++ uses libc++ standard library, which uses g++ uses libstdc++ standard library, which uses Used compilers:
In C++, unlike C, standard headers are allowed to 这篇关于为什么Mac上的铿锵声自动包含一些缺失的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< iostream>
包含< istream>
,它定义了不同类型的>>
运算符。 < istream>
想要知道 short
的限制, int
和 streamsize
类型。
$ b clang ++ 使用 libc ++ 标准库,它使用 std :: numeric_limits
< code>< limits> 中的c $ c> class模板。这就是为什么当包含< iostream>
时自动包含此头文件的原因。
< ext / numeric_traits.h>中的类模板
)。这个头文件中还有一个注释,它解释了为什么他们不使用 __ gnu_cxx :: __ numeric_traits
/ code>而不是在< istream>
中使用< limits>
><比特/ istream.tcc> < limits>
:
< limits>
是很大的,我们避免包括它
>铿锵++ --version
Apple LLVM 8.0.0版(clang-800.0.42.1)
$ g ++ --version
g ++(Debian 4.9.2-10)4.9.2在C ++中,与C不同的是,标准头文件被允许使用以下语法:
#include
其他标准标题。这有时会导致类似你看到的那样的神秘错误:一个编译器的< iostream>
包含< limits>
而另一个没有。解决方案是始终包含您使用的任何名称所需的标头。在这种情况下,这意味着在代码中对 #include< limits>
>进行编译,即使编译器可以像编译器一样编译好。在 #include
中已经有一个头文件,这对于两个编译器都没问题。它有时很烦人,但事情就是这样。<limits>
on Mac, while g++ shows errors about it on Linux. Now I wonder why clang does it, and gcc not. And how I can force clang to not do it.#include <iostream>
using namespace std;
int main()
{
cout << "int max: " << numeric_limits<int>::max() << endl;
}
<iostream>
includes <istream>
, which defines >>
operator for different types. <istream>
wants to know limits for short
, int
and streamsize
types. std::numeric_limits
class template from <limits>
in <istream>
for this purpose. That's why this header is included automatically when <iostream>
is included.__gnu_cxx::__numeric_traits
class template from <ext/numeric_traits.h>
instead of using <limits>
in <istream>
(<bits/istream.tcc>
). There is also a comment in that header which explains why they don't use <limits>
:
<limits>
is big and we avoid including it> clang++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
$ g++ --version
g++ (Debian 4.9.2-10) 4.9.2
#include
other standard headers. That sometimes leads to mysterious errors like the ones you're seeing: one compiler's <iostream>
includes <limits>
and the other doesn't. The solution is to always include the headers needed for whatever names you use. In this case that means to #include <limits>
in your code, even though it compiles okay as is with one compiler. There's no harm in #include
a header that's already been pulled in, so that's okay with both compilers. It's annoying sometimes, but that's just the way things are.