In *.h header files of a C library, should one declare functions
extern void f();
// or only
void f();
- when using only in
C - when using from
C++.
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.
There’s [almost] never any need to use the keyword
externwhen declaring a function, either in C or in C++. In C and in C++ all functions have external linkage by default. The strange habit of declaring functions in header files withexternprobably has some historical roots, but it has been completely irrelevant for decades already.There’s one [obscure?] exception from the above in C, which is probably not directly related to what you are asking about: in C language (C99) if in some translation unit a function is defined as
inlineand also declared asextern(an explicitexternis used) then the inline definition of that function also serves as an external definition. If no declarations with explicitexternare present in the translation unit, then the inline definition is used as “internal” definition only.P.S. There’s such thing as
extern "C"in C++, but that is a completely different matter.