53

Should functions be made extern in header files? Or are they extern by default?

For example, should I write this:

// birthdays.h
struct person find_birthday(const char* name);

or this:

// birthdays.h
extern struct person find_birthday(const char* name);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • 1
    An answer that is much more understandable than the ones below is at https://stackoverflow.com/a/18173554/4756270, section "extern applied to functions" – Luca Ceresoli Nov 13 '19 at 14:53

6 Answers6

23

From The C Book:

If a declaration contains the extern storage class specifier, or is the declaration of a function with no storage class specifier (or both), then:

  • If there is already a visible declaration of that identifier with file scope, the resulting linkage is the same as that of the visible declaration;
  • otherwise the result is external linkage.

So if this is the only time it's declared in the translation unit, it will have external linkage.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 22
    a bit confusiong – Geremia Feb 14 '16 at 02:41
  • 1
    So, if you first declare a function as `static` inside a header file, and then place another `extern` declaration in the same file below, the latter declaration is ignored. Otherwise omitting the specified will implicitly make it `extern`. But it's very important to use `extern` for **variables** in header files, so people sometimes prefer to be explicit and always use `extern` (even when omitting for functions would imply the same linkage). – Lou Jul 20 '17 at 07:42
21

They are implicitly declared with "extern".

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
tur1ng
  • 3,139
  • 5
  • 24
  • 31
  • is it same for variables as well? – Aravind May 19 '21 at 05:16
  • 2
    @Aravind — no, variables are not implicitly declared `extern`; you must explicitly add the `extern` in front of variable declarations. As I note in my answer, I prefer the `extern` in front of function declarations too, for symmetry with the few global variable declarations, but the compiler does not need `extern` before function declarations and many people prefer to leave it out from function declarations. Function definitions can be recognized by the compound statement or function body (enclosed in `{ … }`) instead of a semicolon after the function signature. – Jonathan Leffler Jul 20 '21 at 16:24
20

Functions declared in headers are normally (unless you work really hard) extern. Personally, I prefer to see the explicit keyword there - but the compiler doesn't need it. It reminds the readers that they are extern, and since humans are more fallible than computers, I find the reminder helps.

With variables, it is important to use the extern keyword (and no initializer) in the header file. Consequently, for symmetry with the (very few) global variables declared in headers, I use extern with the function too - even though it is strictly not necessary.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    I add the explicit `extern` a well; declarations in header files are either `extern` or `static inline` for functions and `extern` or `static const` for variables; anything else is rarely needed – Christoph Jul 30 '10 at 07:50
16

No, functions declared in header files do not need to be declared extern.

But variables defined in a .h header and then #included in multiple .c files will need to be declared extern.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
2

I never bother with the "extern" in my source code, but some people do. To my mind, having extern before variables but not functions makes it more visually obvious which things are functions and which things are variables (possibly including function pointers). I think a lot probably depends on how the declarations in the .h file are created, and how they relate to the main .c file. I usually start by typing in the .h file prototypes, and then copy/paste to the .c file and add the function body (striking the semicolon at the end of the prototype), so "extern" would require have to be added to the header file or struck from the main .c file after the copy/paste.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • "extern" is necessary when you need to directly share global variables among multiple source files (modules). You can avoid this with having access functions but these come at a cost of course. – Kostas Sep 26 '17 at 09:27
0

"Should functions be made extern in header files? Or are they extern by default?"

Should functions be made extern in header files?

  • That is an opinion and differs even among open source.

The reason why I do is simple. If not declared in a header, you can declare that function extern in the file you wish to use it, right before using it. If you put it in the header file, that will be included from the file in which the function is used.

In that respect, I think it is more correct to have the extern in the header. However, compilers do that intentionally because there were bad programmers who didn't know any better.

Or are they extern by default?

  • Function declarations are implicitly extern by default.
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
dW1972
  • 1
  • 1