1

As far as i know, inline functions in C work pretty the same way as in C++ when used in single translation unit and there's no need to dive into extern inline difficulties in such a case. However, the following program including three files seems not to compile in C and I am struggling to figure out why.

f.h

int f(); inline int g(); 

f.c

#include "f.h" inline int g() { return 5; } int f() { return 3 + g(); } 

main.c

#include "f.h" #include <stdio.h> int main() { printf("%d", f()); return 0; } 

The linker

tells that there is an undefined reference to g. However, as g is used only in f.c file, I can not define where the problem lies exactly.

2
  • You need extern inline int g(); in one translation unit, in ISO C Commented Dec 25, 2019 at 22:17
  • It would help to show your build command, e.g. as GNU C behaves differently to ISO C Commented Dec 25, 2019 at 22:18

1 Answer 1

2

From the C Standard (6.7.4 Function specifiers)

7,,,For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit

In your project the function g is declared in the translation unit with main.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.