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>
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 anextern "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 anextern "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:So the standard, non-deprecated, canonical way to use (e.g.)
printfin C++ is to#include <cstdio>and then invokestd::printf.