ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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".
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.
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?
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.
"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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.