christian.bau wrote:
On Oct 9, 6:49 pm, regis <regis.barbanc. ..@free.frwrote :
Greetings,
static int mode= 1;
static int half0 (int x) { return x / 2; }
static int half1 (int x) { return x >1; }
inline int half (int x) { return mode ? half1 (x) : half0 (x); }
Since version 4.3, it seems that gcc with -std=c99 warns:
mode is static but used in inline function half which is not static
half1 is static but used in inline function half which is not static
half0 is static but used in inline function half which is not static
C99 Standard draft 6.7.4.3: "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."
The reason for this requirement is that a compiler is free to create
both an actual function with external linkage that has only one
definition, and seperate inline code in every translation unit that
refers to the function. If it isn't meant to be used in mutliple
translation units, it shouldn't have external linkage.
The real function and the inlined equivalents are supposed to have
identical behavior, so it doesn't matter which one is used, and the
compiler is free to make a different choice at different locations in
your code. However, if the function refers to identifiers with
internal linkage, or defines objects of static storage duration, it
could make a difference which one was used. In your case, half1 and
half2 should be identical in every translation unit, so they're not
actually a problem. However, the standard is not intended to require
compilers to be smart enough to notice that fact; referring to them is
illegal.
The real problem is that there will be a separate copy of mode in each
translation unit, so the behavior of different copies of half() could
be different. If you want them to be different, you should declare
half() as static. If you want them to all be the same, remove 'static'
from half0, and half1, and replace static with 'extern' for mode, and
make sure that an actual definition for mode is provided in one an
only one translation unit.