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++
.
In *.h
header files of a C
library, should one declare functions
extern void f();
// or only
void f();
C
C++
.There's [almost] never any need to use the keyword extern
when 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 with extern
probably 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 inline
and also declared as extern
(an explicit extern
is used) then the inline definition of that function also serves as an external definition. If no declarations with explicit extern
are 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.
In header files of a C library, should one declare functions:
extern void f(); // or only void f();
In a C++ program, the functions are declared as functions returning no value and taking no arguments.
In a C program prior to C23, the functions are declared as functions returning no value and taking an indeterminate but not variable-length list of arguments. In C23, the meaning is the same as in C++.
To get the 'no arguments' meaning in C, use one of:
extern void f(void);
void f(void);
The same notation also means the same thing in C++, though for pure C++ code, using void
in the argument list is not idiomatic (do not do it in pure C++ code).
Tricky, but the normal rule would that you should declare the functions to C++ code as extern "C"
. To use the same source code for both, you then need to test the __cplusplus
macro. You'd normally do something like:
#ifdef __cplusplus
#define EXTERN_C extern "C"
#define EXTERN_C_BEGIN extern "C" {
#define EXTERN_C_END }
#else
#define EXTERN_C /* Nothing */
#define EXTERN_C_BEGIN /* Nothing */
#define EXTERN_C_END /* Nothing */
#endif
EXTERN_C void f(void);
EXTERN_C_BEGIN
void f(void);
int g(int);
EXTERN_C_END
The options and variations are manifold, but the header can be used by both C and C++.
The macros would normally be defined in one general-purpose header that's used everywhere, and then the particular header would ensure that the general purpose header is included and then use the appropriate form of the macro.
Formally, there is no need for the extern
notation before a function declaration. However, I use it in headers to emphasize that it is a declaration of an externally defined function, and for symmetry with those (rare) occasions when there is a global variable declared in the header.
People can, and do, disagree over this; I go with the local rules — but when I'm the rule-maker, the extern
is included in a header.
For general use declare as
#ifdef __cplusplus
extern "C" {
#endif
void f(void);
#ifdef __cplusplus
}
#endif
Otherwise, extern
is obsolete.
The latter is perfectly fine, since it's only a function definition, which tells those, who include this header: 'There's a function with this prototype somewhere around here'
In this context, functions differ clearly from variables, but that's a different matter. Make sure though, that you do not include the function body, unless you declare it 'inline' or as part of a class definition (C++) or as a 'template function' (also C++).
Specifying extern in function prototype has no effect, since it is assumed by default. Whenever a compiler sees a prototype, it assumes a function is defined somewhere else (in the current or another translation unit). This holds for both of the languages.
The following thread has some useful comments in general about extern.