2
\$\begingroup\$

Perhaps I don't fully understand extern. With the XC8 C compiler, it seems like I can get away without using it.

In the file keypad.c I have a function signed char keypadGetPressedKeyLabel(void). In the header file keypad.h I have:

signed char keypadGetPressedKeyLabel(void);

I thought I would have to use extern, ie:

extern signed char keypadGetPressedKeyLabel(void);

The project builds without extern. And it works. Might this due to the fact that keypad.c and keypad.h are all part of the project? I'm using MPLABX.

\$\endgroup\$
1
  • \$\begingroup\$ Are any other files using keypad.h? \$\endgroup\$ Commented Nov 11, 2014 at 20:05

4 Answers 4

2
\$\begingroup\$

extern is not strictly necessary for function prototypes in .h files - whether or not the function is actually used in one or many different .c files in your project.
Whether you have extern int foo(void); or just int foo(void); in your .h your compiler will read is as a function prototype either way.

Its necessary for global variables though since unlike functions they don't have prototypes.
So if you have an int thing; in one .c file and you want to use it in another .c file then you'll need an extern int thing; to tell the compiler that it does exist 'somewhere' in your project.

\$\endgroup\$
1
  • \$\begingroup\$ Correct. Also, you can include initialization only on the definition of the variable e.g. int a = 0; it is an error to repeat this again in an external declaration e.g. extern int a = 0; // error \$\endgroup\$
    – tcrosley
    Commented Nov 11, 2014 at 20:37
2
\$\begingroup\$

Functions are extern by default in C; that's why it works without the keyword.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This is really the correct answer, but I think it would also benefit from a brief explanation of what extern actually means. \$\endgroup\$
    – NickHalden
    Commented Nov 11, 2014 at 21:08
  • \$\begingroup\$ Great answers; PICs XC8 compiler uses extern looking for external files, not just as a global declaration. \$\endgroup\$
    – Leroy105
    Commented Jan 1, 2017 at 18:04
1
\$\begingroup\$

extern is really a sign to the linker, that the symbol is defined in a different object file. As you only seem to have one object file, it doesn't make any difference whether it's there or not.

\$\endgroup\$
1
\$\begingroup\$

extern is largely obsolete for functions in other CUs in many of the smaller simpler compilers. If a symbol isn't found locally it will be searched for in other CUs and libraries. You provide a function prototype, but not the actual function, in the CU where you want to use it.

However, extern is required for variables that are shared between CUs. You can't define a prototype for a variable, so the extern keyword in this situation has to be used to create the variable equivalent of a function prototype.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.