Why would you use `extern void my_func();` rather than including `my_utils.h`?
I'm working on some code I didn't write and noticed that there are many extern void my_func();
.
My understanding is that extern
in for global variables, not for functions.
Is there a practical reason to declare a function as extern
rather than putting it in a header f开发者_高级运维ile and including that? Or is this just a stylistic choice?
This is only needed if, for some reason, the header file doesn't declare the function. And extern
is always unnecessary for functions, as functions are always extern
by default.
One use of extern
functions is that suppose you have two modules: module_a (implemented in module_a.h
and module_a.c
files), module_b (implemented in module_b.h
and module_b.c
files). Now you want a specific function of module_b to use in module_a. But you don't want to expose all the functionality of module_b into module_a. So that case instead of #include "module_b.h"
you can extern
the required function prototype only.
Isn't it enough to declare prototype in your *.c file before use of function, instead of including whole header file ? No need to use extern in any case for functions. I have not try yet but it suppose to work that way.
精彩评论