包含< cstdio>的正确顺序和< stdio.h&gt ;? [英] Correct order for including both <cstdio> and <stdio.h>?

查看:217
本文介绍了包含< cstdio>的正确顺序和< stdio.h&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用系统特定的功能,例如 ftello()(根据POSIX标准在 stdio.h 中定义)。
我也需要使用标准的C ++特性,例如 std :: sprintf()(根据ISO C ++标准在 cstdio 中定义)。

$ b AFAIK,包括< cstdio> 并不能保证定义非标准C ++的东西,所以我想我必须同时包含。
我很久以前就读过(例如)使用gcc可能会出现包含文件顺序问题。



那么,什么是正确的要包括< cstdio> < stdio.h>
我正在寻找一个尽可能跨平台的解决方案(至少对于gcc,suncc,intel c ++ / linux和mingw)。

解决方案

好了,经过一番研究后,我终于得出了一个结论,即首先包括C ++头文件,稍后C头文件是正确的事情。
例如,考虑以下C ++ 0x头(来自gcc):
$ b $ / usr / include / c ++ / 4.3 / tr1_impl / cstdint: p>

  
// ...
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
# include_next< stdint.h>
// ...

它的作用是定义两个C99宏,只有包含C99 stdint.h头文件。原因是在C99中,stdint.h的一些功能是可选的,并且只有在定义了这些宏时才可用。但是,在C ++ 0x中,所有stdint.h功能都是必需的。
现在,如果我先包含C99 stdint.h,然后再包含cstdint,那么由于stdint.h中的头文件守护程序,我不会获得强制性的C ++ 0x功能。
有人可能会争辩说这是编译器厂商的错,但那是不正确的。 stdint.h是一个系统绑定的头文件(在这种情况下是glibc),它是一个C99头文件,不知道任何关于C ++ 0x(它毕竟可以是旧系统)或gcc。编译器不能真正修复所有的系统头文件(在这种情况下总是在C ++模式下启用这些功能),但它必须在这些系统上提供C ++ 0x支持,因此供应商使用此解决方法。


I need to use system-specific functions, e.g. ftello() (defined in stdio.h as per POSIX standard). I also need to use standard C++ features, e.g. std::sprintf() (defined in cstdio, as per ISO C++ standard).

AFAIK, including only <cstdio> doesn't guarantee defining non-standard-C++ stuff, so I guess I have to include both. I've read a long time ago that (for example) with gcc there may be problems with the include file order.

So, what is the correct order for including both <cstdio> and <stdio.h>? I'm looking for a solution which is as cross-platform as possible (at least for gcc, suncc, intel C++/linux and mingw).

解决方案

OK, after some more reasearch I finally came to a conclusion that including the C++ header first, C header later is the correct thing to do. For example, consider the following C++0x header (from gcc):

/usr/include/c++/4.3/tr1_impl/cstdint:


// ...
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include_next <stdint.h>
// ...

What it does is that it defines two C99 macros, and only then includes the C99 stdint.h header. The reason is that in C99, some of the features of stdint.h are optional, and only available if those macros are defined. However, in C++0x, all stdint.h features are mandatory. Now, if I included the C99 stdint.h first, cstdint later, I wouldn't get the mandatory C++0x features because of the header guards in stdint.h. One could argue that this is the compiler vendor's fault, but that would be incorrect. stdint.h is a system-bundled header (from glibc in this case), which is a C99 header and doesn't know anything about C++0x (it can be an old system, after all) or gcc. The compiler can't really fix all the system headers (in this case to always enable those features in C++ mode), yet it has to provide C++0x support on these systems, so the vendor uses this workaround instead.

这篇关于包含&lt; cstdio&gt;的正确顺序和&lt; stdio.h&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆