git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Rationale behind 'extern' on protypes in .h files

From
Stefan Beller <stefanbeller@googlemail.com>
Date
Dec 22, 2013, 17:11 UTC
Message-ID
<52B71D24.4000207@googlemail.com>
In-Reply-To
<CAKTJ_1zecXP03k_2YRnm_26n=anxkG6=k+isZxnnjWgfec70LA@mail.gmail.com>
On 22.12.2013 16:51, Ravi Shekhar Jethani wrote:
Show 16 quoted lines
> 
> Now, my real question :
> 1) I cannot understand the reason behind making function prototypes as
> extern. What purpose does this serve? AFAIK we put definition in a .c
> file and the prototype in a .h thats it.
> 
> 2) Why are some  prototypes in some of the .h file are extern and
> others are not?
> 
> Thank you guys for reading through. Any suggestions are humbly welcome.
> Ravi S. Jethani
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

That's an interesting question. From my understanding there is no difference for functions declarations being set to extern or not, because extern is the default on functions. It is however important for variables to mark them extern if they're in a header file.

After a quick research on the web, it may be there for historical reasons. Back then, when there were one pass compilers, the compiler needed to know where the function is to be found. (extern was not default at these times?)

Another reason I could make up, would be to indicate, whether the function is in the c file having the same name as the header file. For example in builtin.h there are all the functions declared extern, as the implementation of the functions are found in builtin/*.c In another header cache-tree.h there are function declarations without explicit extern keyword, and the implementation is found in the cache-tree.c file (${SAME-NAME-AS-HEADER}.C) This is however not always the case as found in archive.{c,h}

I'd be also interested in knowing the real reason for this rationale.

Thanks, Stefan

Previous: Ravi Shekhar JethaniNext: Jed Brown
Message 2 of 5 in “Rationale behind 'extern' on protypes in .h files”
  1. Ravi Shekhar JethaniDec 22, 2013
  2. Stefan BellerDec 22, 2013
  3. Jed BrownDec 22, 2013
  4. Ravi Shekhar JethaniDec 23, 2013
  5. Jed BrownDec 23, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.