Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extern functions in C vs C++

Tags:

c++

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++.
like image 903
Cartesius00 Avatar asked Jul 29 '12 20:07

Cartesius00


People also ask

Can you extern a function in C?

The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files. In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.

What does extern C mean in C?

The extern must be applied to all declarations in all files. (Global const variables have internal linkage by default.) extern "C" specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block.

Are functions extern by default C?

“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.

Is extern necessary in C?

You do not necessarily "need" extern for variables. When C was invented Unix linkers were also written, and they advanced the art in unheralded but clever ways. One contribution was defining all symbols as small "common blocks".


2 Answers

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.

like image 109
AnT Avatar answered Sep 21 '22 05:09

AnT


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.

like image 45
Jonathan Leffler Avatar answered Sep 19 '22 05:09

Jonathan Leffler