3

As I understand it, function prototypes are intrinsically extern and adding the keyword to them will not change functionality. I was looking at the source code for the Linux Kernel and came across the following:

extern bool console_suspend_enabled;

/* Suspend and resume console messages over PM events */
extern void suspend_console(void);
extern void resume_console(void);

int mda_console_init(void);
void prom_con_init(void);

void vcs_make_sysfs(int index);
void vcs_remove_sysfs(int index);

As you can see, some functions have extern prefixed and some do not. This seemed to be present in a number of header files across the project making me wonder: is this just an inconsistency or is it for some sort of (old) compiler compatibility reason?

Source: https://github.com/torvalds/linux/blob/master/include/linux/console.h#L188

  • If you try compiling it you'll probably get a few warnings telling you that you are calling an implicit function or something like that ... by putting extern on the file using rhe function then the linker KNOWS that it needas to link to that function ... that's why you sometimes create the prototypes for the functions in a header file ... so you include the header and the linker knows how to link everything ... have you lloked at queel_solar's code? it has a quite interesting use of extern and typedefs for creating an API – morcillo Sep 13 '17 at 15:51
  • @morcillo: Using a name without preceeding declaration is not allowed. – too honest for this site Sep 13 '17 at 16:02
  • That's bad practice, but valid. `extern` is not required for a declaration, but there are errors not recognised is you don't. Sems like some author(s) just copy/pasted and were a bit lazy not adding `extern`. – too honest for this site Sep 13 '17 at 16:04
  • @Olaf good to know, so let if you don't mind I'll use this oportunity to learn. Does that mean that when I call a function without a prototype in a header or an extern and my compiler gives me a warning about it, but still links to the correct function ... then that's just the compiler that works that way? sorry for pestering you in the comment section – morcillo Sep 13 '17 at 16:16
  • @morcillo: You should learn what headers are and what not. It is completely irelevant if something is in an included header or pasted in the orinal text. And you have to have a function declared before you can use it. That is not related to the use of storage class specifiers. All this will be explained in a good C book. I'd strongly recommend to get one if you are not sure. As a sidenote: a compiler does not link. – too honest for this site Sep 13 '17 at 16:52
  • A singular advantage to using `extern` before a function declaration is textual. I find it easier to find that header's functions/objects via a "extern" search. As with such optional adornments, best to follow your group's coding standard concerning this issue. – chux - Reinstate Monica Sep 13 '17 at 16:53
  • @chux: It also reports if you have an `extern` declared function later `static` specified. – too honest for this site Sep 13 '17 at 16:54

1 Answers1

1

is this just an inconsistency or is it for some sort of (old) compiler compatibility reason?

It's certainly an inconsistency in coding style. But this is harmless since both are equivalent. The presence or absence of extern keyword for function declarations makes no difference - extern is optional for function declarations.


On the other hand, not providing any declaration can lead to subtle bugs such as implicit function declarations (by the way,implicit declarations are not valid since C99 - Are prototypes required for all functions in C89, C90 or C99?) if the function defined in one compilation unit is used in other compilation unit(s). Using a header file to provide declarations across multiple compilation units is the common practice.

P.P
  • 117,907
  • 20
  • 175
  • 238