// lib.h
#pragma once
static inline int static_inline(int x) { return x; }
inline int extern_inline(int x) { return static_inline(x); }
// lib.c
#include "lib.h"
extern inline int extern_inline(int x);
// app.c
#include "lib.h"
When compiling app.c, gcc gives this warning:
'static_inline' is static but used in inline function 'extern_inline' which is not static.
Why is it a problem? My reasoning is: in app.c, if extern_inline
is not inlined, then it becomes a function call, and its implementation detail (calls static_inline
) can be ignored. If extern_inline
is inlined, then it has the full definition, and definition in other translation unit can be ignored too.
The C standard explicitly forbids extern inline functions from defining static objects or calling other inlined non-extern functions:
6.7.4 Function specifiers
[...]
- An inline definition of a function with external linkage shall not contain a definition of a modifiable object with static storage duration, and shall not contain a reference to an identifier with internal linkage.
Reference: C99 draft WG14/N1256 section 6.7.4 point 3.
Whether or not it may technically be possible is another question. It definitely looks like it could work, however since the standards forbids it, the compiler is allowed to produce and optimize code assuming this should not happen.