Why 'extern' on function prototype
I'm currently examining the code of cpufreqd, and there is a module that implements a linked list. In the header "list.h" there is this function prototype:
Code:
extern int list_free_sublist(struct LIST *l, struct NODE *n); Any ideas? |
Is it because its used with a function pointer?
ie. Code:
int (*func_p)(struct LIST *, struct NODE *); |
Well, it doesn't really matter for functions in headers. It's just their way of saying: this is a pulic function that will be defined elsewhere.
|
Quote:
|
Function pointers and extern are unrelated. You need extern if you have a helper function that you want to be seen in other cpp files that normally could not.
|
extern means the fn def/content is in another file.
|
I think, earlier days, it had to with the scope of the current source code. If is defined elsewhere (in relation to the curent source code), we had to declare it as "extern". But this is obviously too simple. Most functions are defined in libraries. So is the scope related to the current compiled object?
End |
why extern on function prototype
Generally extern used to tell compiler that function is defined elsewhere or function defination will be available at linking time. So source file can be compiled even if function defination are not available.
This also work if the function defination is available in form of library or object file. ~deepak |
The standard says
"If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external." So for functions having neither extern not static specifiers is the same as if extern was specified. Therefore, it's good practice to declare functions declared and define in a source file to be used only there as static to reduce the size of the resulting executable. In headers it doesn't make any difference, as I said in my first reply to this thread, to use extern when declaring functions. They're already extern. |
|
You don't need to put extern before a function prototype in order to use it with a function pointer. As far as I know, "extern" is totally optional with function prototypes. I guess some coders use it to make it explicit that the function is in another source file.
|
Quote:
|
All times are GMT -5. The time now is 12:41 AM. |