-1

I'm not sure whether the following is a bug in the C standard, a bug in GCC, or some weird behavior I just misunderstand, but the following complains about an implicit declaration of foo:

static inline void foo(void);

inline void bar(void) {
    foo();
}

Any thoughts? Thanks.

Note: If you want the correct answer, search for mine rather than the other ones, regardless of votes.

  • 3
    can you post the exact complaints – µtex Nov 25 '16 at 05:45
  • 1
    Also you are using foo() but never defined. – µtex Nov 25 '16 at 05:47
  • I did post the exact complaint: an implicit *declaration* is used instead of the one provided. Definitions have nothing to do with the matter, which is why I didn't provide one. If I provided one, it'd still go unused. At any rate, I've answered the question. – Bogdan Barbu Nov 25 '16 at 06:26

2 Answers2

3

Nevermind, I've found the correct answer.

Even though the code makes logical sense for the single translation unit case, the standard seems to explicitly forbid this corner case (it'd be bad practice anyway, since refactoring could lead to problems if foo and bar are moved to separate TUs---since foo wouldn't be bar's TU's scope).

C11's §6.7.4p3 says:

An inline definition of a function with external linkage shall not contain a definition of a modifiable object with static or thread storage duration, and shall not contain a reference to an identifier with internal linkage.

1

You are declaring foo, but never defining it. The error I get is inline function ‘void foo()’ used but never defined, which tells you exactly that.

Henning Koehler
  • 2,456
  • 1
  • 16
  • 20
  • The warning that I don't know enough about to explain is: `warning: ‘foo’ is static but used in inline function ‘bar’ which is not static`. I'll assume you can't use static functions in non-static function, but why? Scope rules? – Chirality Nov 25 '16 at 05:54
  • I'm just not defining it in that snippet. But as you can see, it's trying to use an implicit declaration anyway so that is not the issue. I've already provided the answer below. – Bogdan Barbu Nov 25 '16 at 06:07