Do I need an extern “C” block to include standard POSIX C headers?

前端 未结 6 1835
一向
一向 2020-11-29 11:21

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++.

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 11:51

    The behavior of and 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 , , 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 , , etc., and they are specified by section 17.4.1.2 (3) of the C++ standard, which says:

          
          
    
    

    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 and then invoke std::printf.

提交回复
热议问题