LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why 'extern' on function prototype (https://www.linuxquestions.org/questions/programming-9/why-extern-on-function-prototype-412674/)

Hko 02-07-2006 01:19 PM

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);
Now I wonder: What could be the reason to put extern in front of a function prototype? Since it's a function any other module is able to call it already without "extern".

Any ideas?

dmail 02-07-2006 02:09 PM

Is it because its used with a function pointer?
ie.
Code:

int (*func_p)(struct LIST *, struct NODE *);
extern int list_free_sublist(struct LIST *l, struct NODE *n);
func_p = list_free_sublist;


Hivemind 02-07-2006 02:10 PM

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.

Hko 02-07-2006 03:54 PM

Quote:

Originally Posted by dmail
Is it because its used with a function pointer?

Yes, function pointers are in involved. I didn't know extern is needed in that case. Thanks.

tuxdev 02-07-2006 07:31 PM

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.

chrism01 02-07-2006 10:17 PM

extern means the fn def/content is in another file.

AnanthaP 02-08-2006 06:52 AM

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

dkukrety 02-08-2006 07:30 AM

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

Hivemind 02-08-2006 08:25 AM

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.

bigearsbilly 02-08-2006 10:32 AM

google is your friend ;)

http://www.eskimo.com/~scs/cclass/int/sx10c.html

llama_meme 02-08-2006 01:06 PM

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.

Hko 02-08-2006 04:50 PM

Quote:

Originally Posted by bigearsbilly

Good link!


All times are GMT -5. The time now is 12:41 AM.