user8585867
user8585867

Reputation:

Is there any purpose in declaring extern on specific functions only, then not on others (for compatibility)?

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

Upvotes: 3

Views: 246

Answers (1)

P.P
P.P

Reputation: 121387

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.

Upvotes: 1

Related Questions