Why would you use `extern void my_func();` rather than including `my_utils.h`?

21,398

This is only needed if, for some reason, the header file doesn't declare the function. And extern is always unnecessary for functions, as functions are always extern by default.

Share:
21,398
coolaj86
Author by

coolaj86

<3 Go, Rust, Node, VanillaJS ✓ Python, C ✗ Java, C++ </3 PHP (and therefore React) #FathersMatter #BlackFathersMatter

Updated on September 14, 2020

Comments

  • coolaj86
    coolaj86 over 3 years

    I'm working on some code I didn't write and noticed that there are many extern void my_func();.

    My understanding is that extern in for global variables, not for functions.

    Is there a practical reason to declare a function as extern rather than putting it in a header file and including that? Or is this just a stylistic choice?

  • Donotalo
    Donotalo over 13 years
    functions are always extern by default in C? are you sure??
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    @Donotalo: Pretty sure, but it is midnight, so who knows what tiredness has done to my brain.
  • Donotalo
    Donotalo over 13 years
    i'm working for micro controller for 2 years. the compiler i use is C. though not completely ANSI C, and many features of C99 are absent, but to call a non-static function defined in another file we need to extern its prototype, or include the header.
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    @Donotalo: Then your compiler is broken. #include-ing a header file with function prototypes is equivalent to writing the prototypes directly into your source file, and you don't see header files full of explicit extern functions!
  • Donotalo
    Donotalo over 13 years
    no we don't put extern functions in header. we have to put it in .c files where they are called.
  • Donotalo
    Donotalo over 13 years
    i got it what do you mean by: functions are always extern by default. at first i thought functions can be called across implementation files without including header and without externing it. i was wrong.
  • Oliver Charlesworth
    Oliver Charlesworth over 13 years
    As discussed in the comments to my answer, use of extern is unnecessary.
  • Donotalo
    Donotalo over 13 years
    @Oli: I've just tested this feature using VS2008. It didn't work except extern. Any idea why? I have 3 files: main.cpp (contains main()), functions.h and functions.cpp. functions.cpp defines a function (named fun()) that just returns 0. from main(), i called cout << fun() << endl; compiler says fun() is undeclared. then i wrote extern int fun(); before main(). then it worked.
  • 0xbadf00d
    0xbadf00d almost 10 years
    @Donotalo A function declaration R func(T arg) tells the compiler that there is a function called func which returns a value of type R and has a formal parameter of type T called arg. This allows the use of the function name in the context of the declaration. The linker is responsible for resolving such a name and find the corresponding function definition which might can be found in a different compilation unit.