LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2006, 01:19 PM   #1
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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?
 
Old 02-07-2006, 02:09 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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;

Last edited by dmail; 02-07-2006 at 02:12 PM.
 
Old 02-07-2006, 02:10 PM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 02-07-2006, 03:54 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Original Poster
Rep: Reputation: 111Reputation: 111
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.
 
Old 02-07-2006, 07:31 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 02-07-2006, 10:17 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
extern means the fn def/content is in another file.
 
Old 02-08-2006, 06:52 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
Old 02-08-2006, 07:30 AM   #8
dkukrety
LQ Newbie
 
Registered: Feb 2006
Posts: 1

Rep: Reputation: 0
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
 
Old 02-08-2006, 08:25 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 02-08-2006, 10:32 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
google is your friend

http://www.eskimo.com/~scs/cclass/int/sx10c.html
 
Old 02-08-2006, 01:06 PM   #11
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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.
 
Old 02-08-2006, 04:50 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Original Poster
Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by bigearsbilly
Good link!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How many prototype of function main in c? ypzhuang Programming 9 12-13-2008 07:53 AM
GLib g_date_set_dmy () prototype info. Thetargos Programming 0 07-15-2004 06:46 PM
specifying a prototype dhanakom Programming 2 09-02-2003 03:07 AM
gcc: compilation with extern inline function philipsyyy Linux - Software 0 10-13-2002 11:52 PM
Assembly for Rapid Prototype mikeshn Programming 1 03-09-2002 04:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration