1

Following these two links: Should C declarations match definition including keywords and qualifiers such as "static", "inline", etc

and

https://www.greenend.org.uk/rjk/tech/inline.html

They say the following is the way to use inline functions in C:

a.c

#include <stdio.h> inline int add(int a , int b){ return a+b; } int main(){ printf("%d\n", add(1,2)); return 0; } 

b.c

int add(int a , int b); inline int add(int a , int b){ return a+b; } 

Then compile with: gcc a.c b.c.

Now here is my confusion:

Why does adding a declaration of int add(int a , int b); to b.c cause a symbol generation? I thought only function definitions create symbols in a translation unit not their declarations. Why is this backwards? Is inline a special case?

It's really wired that I have to have an external symbol to work with inline functions. Why can't C compile a.c on it's own! (I know gnu89 and C++ do, wondering why c99 was coded like this to begin with).

9
  • b.c contains both a declaration and a definition. Commented Jun 15, 2020 at 22:39
  • It doesn't tho, inline declaration doesn't emit any symbol technically. Commented Jun 15, 2020 at 22:41
  • inline functions don't emit a symbol unless they're also declared extern. Commented Jun 15, 2020 at 22:42
  • @Barmar Ok so why does the declaration in b.c cause a symbol generation? this is weird and not like other C practices. Commented Jun 15, 2020 at 22:44
  • I see your point, I'm not sure Commented Jun 15, 2020 at 22:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.