我需要一个extern“C”块包括标准C头? [英] Do I need an extern "C" block to include standard C headers?

查看:137
本文介绍了我需要一个extern“C”块包括标准C头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 externC{} 块,以在C ++程序中包含标准C头。只考虑在C ++中没有对应项的标准C头。



例如:

 code> externC{
#include< fcntl.h>
#include< unistd.h>
}


解决方案

<$ c C ++中的$ c>< fcntl.h> 和< unistd.h> 不是由标准指定的(因为它们也不是部分C89标准)。也就是说,我从来没有见过一个平台,他们(a)存在,(b)实际上需要包装在 externC b
$ b

< stdio.h> < math.h> ,而其他标准C头由C ++ 03标准的D.5节指定。它们不需要 externC包装器块,并且将它们的符号转储到全局命名空间中。但是,附件D中的所有内容都已废弃。



这些标题的规范C ++形式是< cstdio> < cmath> 等,并且它们由C ++标准的第17.4.1.2(3)节指定:

 < cassert> < ciso646> < csetjmp> < cstdio> < ctime> < cctype> < climits> 
< csignal> < cstdlib> < cwchar> < cerrno> < clocale> < cstdarg> ;. < cstring>
< cwctype>




除第18至27条所述外,
cname应与相应的标题name.h的相同,如ISO / IEC 9899:1990编程语言C(第7条)中规定的

ISO / IEC:1990编程语言-C修改1:C完整性,(条款
7),如果合适,如同通过包含。然而,在C ++标准库
中,声明和定义(除了
定义为C中的宏之外的名称)在
命名空间std的命名空间范围(3.3.5)中。


因此,使用标准的,非弃用的,规范的方式(例如) printf 在C ++中是 #include< cstdio> 然后调用 std :: printf


Do I need an extern "C" {} block to include standard C headers in a C++ program. Only consider standard C headers which do not have counterparts in C++.

For example:

extern "C" {
 #include <fcntl.h>
 #include <unistd.h>
}

解决方案

The behavior of <fcntl.h> and <unistd.h> in C++ is not specified by the standard (because they are also not part of the C89 standard). That said, I have never seen a platform where they (a) exist and (b) actually need to be wrapped in an extern "C" block.

The behavior of <stdio.h>, <math.h>, and the other standard C headers is specified by section D.5 of the C++03 standard. They do not require an extern "C" wrapper block, and they dump their symbols into the global namespace. However, everything in Annex D is "deprecated".

The canonical C++ form of those headers is <cstdio>, <cmath>, etc., and they are specified by section 17.4.1.2 (3) of the C++ standard, which says:

<cassert> <ciso646> <csetjmp> <cstdio> <ctime> <cctype> <climits>
<csignal> <cstdlib> <cwchar> <cerrno> <clocale> <cstdarg> <cstring>
<cwctype>

Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate, as if by inclusion. In the C++ Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.

So the standard, non-deprecated, canonical way to use (e.g.) printf in C++ is to #include <cstdio> and then invoke std::printf.

这篇关于我需要一个extern“C”块包括标准C头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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