简体   繁体   中英

Extern functions in C vs C++

In *.h header files of a C library, should one declare functions

extern void f();

// or only 

void f();
  1. when using only in C
  2. when using from 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.

PS 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();

Issue 1: Semantics

In a C++ program, the functions are declared as functions returning no value and taking no arguments.

In a C program, the functions are declared as functions returning no value and taking an indeterminate but not variable-length list of arguments.

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

Issue 2: Inter-working between C and C++

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.

Issue 3: Style

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.

Effects of the extern keyword on C functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM