0

consider the following header and two variations on the same source file.

// myheader.h
#include <stdio.h>
inline void bla(){
    printf("hello\n");
}

// mysource.c
#include "myheader.h"
void bla();
int main(){
    bla();
}

// mysource2.c
#include "myheader.h"
extern void bla();
int main(){
    bla();
}

Both compile fine and outputs hello as expected. So what does the extern qualifier in mysource2.c add here? When is it needed?

EDIT: I should add that I'm using GCC with standard flags.

user7893856
  • 117
  • 1
  • 7
  • 1
    nothing; functions are `extern` by default unless marked `static` – underscore_d Jul 25 '17 at 11:06
  • Answer: loosely speaking, it is only required if you define the function inside another .c file or similar. – user7893856 Jul 25 '17 at 11:10
  • Actually, maybe [What happens with an extern inline function?](https://stackoverflow.com/questions/17504316/what-happens-with-an-extern-inline-function) would've been better – underscore_d Jul 25 '17 at 11:10
  • I thought the one I accepted answered it neatly. – user7893856 Jul 25 '17 at 11:10
  • Cool, glad I guessed right first time :) – underscore_d Jul 25 '17 at 11:11
  • @user7893856 anyway inline in gcc will be ignored and the compiler will choose the invoking way. You need to use attribute to make it inline (or noinline). Same is with the functions without the inline keyword. Those one can be inlined by the gcc – 0___________ Jul 25 '17 at 11:34
  • Function declaration and definition are different in there syntax. Wherever you don't supply a definition (the part between parentheses {}), it's a declaration, and 'extern' by default. Variable definition can be the same as definition in some cases, so the 'extern' is needed to tell the compiler that this is a declaration, and the definition is supplied elsewhere. – Maged Elghareib Jul 25 '17 at 11:36
  • @PeterJ Presence or absence of the standard `inline` keyword ultimately does not dictate whether or not a given function gets inlined, and given compilers may provide more reliable ways to do so - but I don't think it's true that GCC completely ignores the standard `inline` keyword as you claim. This (somewhat old, but seemingly not superseded) [documentation](https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Inline.html) indicates the keyword can have meaning. I'd argue that - especially now that `-flto` is a thing - `inline` is premature optimisation. But that doesn't mean it's completely ignored – underscore_d Jul 25 '17 at 21:51
  • @underscore_d inline has no effect *at all* when not optimising - when optimising own optimisation gets priority over inline. And in 99% that optimisation incudes inlining and it looks like inline "works". But try with the not trivial pieces of code (long functions, frequently called etc) and you will see what I mean. The only exception is when the function is extern **and** inline and inlining is done during the linking. – 0___________ Jul 25 '17 at 22:03
  • @PeterJ Obviously I wasn't considering the case of no optimisation, since we're talking about optimisation. And so, when optimising, you agree with me that the keyword is not _guaranteed_ to be _completely_ ignored, as you originally stated. – underscore_d Jul 25 '17 at 22:06
  • @underscore_d Practically it is. Do some experiments (but not trivial) - as you sound for me a bit theoretical (please do not link to the gnu website), with a very limited practical (in this field) experience. In couple of large projects it was essential for me to have a 100% control over inlining and only attributes force gcc to exactly as you want. _extern/static_ `inline void xxx(void) __attribute__((always_inline));` – 0___________ Jul 25 '17 at 22:13
  • @PeterJ Yes, I'm being theoretical. If you make a theoretical statement that the keyword is always ignored, then you're going to get a theoretical objection. As for practice, I didn't say it was always used, nor that your alternatives were not better - but again, make a theoretical point, get a theoretical rebuttal. – underscore_d Jul 26 '17 at 08:58
  • @underscore_d `inline` has almost only the practical implications and cannot be considered the same way as other C standard considerations. Theoretically if we abstract from the real implementation inlined and not inlined functions behave exactly the same way and there is no algorithmic difference. – 0___________ Jul 26 '17 at 09:03

0 Answers0