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

前端 未结 6 1839
一向
一向 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:46

    No, you should use the C++ wrapper headers (for instance like ). Those take care of all that for you.

    If it's a header that doesn't have those, then yes, you'll want to wrap them in extern "C" {}.

    ETA: It's worth noting that many implementations will include the wrapper inside the .h file like below, so that you can get away with not doing it yourself.

    #ifdef  __cplusplus
    extern "C" {
    #endif
    
    #ifdef  __cplusplus
    }
    #endif
    

提交回复
热议问题