Is "inline" without "static" or "extern" ever useful in C99?
Asked Answered
L

3

118

When I try to build this code

inline void f() {}

int main()
{
    f();
}

using the command line

gcc -std=c99 -o a a.c

I get a linker error (undefined reference to f). The error vanishes if I use static inline or extern inline instead of just inline, or if I compile with -O (so the function is actually inlined).

This behaviour seems to be defined in paragraph 6.7.4 (6) of the C99 standard:

If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition.

If I understand all this correctly, a compilation unit with a function defined inline as in the above example only compiles consistently if there is also an external function with the same name, and I never know if my own function or the external function is called.

Isn't this behaviour completely daft? Is it ever useful to define a function inline without static or extern in C99? Am I missing something?

Summary of answers

Of course I was missing something, and the behaviour isn't daft. :)

As Nemo explains, the idea is to put the definition of the function

inline void f() {}

in the header file and only a declaration

extern inline void f();

in the corresponding .c file. Only the extern declaration triggers the generation of externally visible binary code. And there is indeed no use of inline in a .c file -- it's only useful in headers.

As the rationale of the C99 committee quoted in Jonathan's answer explicates, inline is all about compiler optimisations that require the definition of a function to be visible at the site of a call. This can only be achieved by putting the definition in the header, and of course a definition in a header must not emit code every time it is seen by the compiler. But since the compiler is not forced to actually inline a function, an external definition must exist somewhere.

Layfield answered 10/6, 2011 at 22:14 Comment(9)
borderline duplicate of stackoverflow.com/questions/5369888/…Bastardize
And also https://mcmap.net/q/16705/-what-does-extern-inline-doPrenomen
@Earlz: Thanks for the link. My question is about the rationale behind the quoted paragraph of the standard, and if there are ever use cases for inline without static and extern, though. Unfortunately, none of these issues is covered in that question.Layfield
@Nemo: I read that question and the answers before I posted mine. Again, I'm not asking "how does it behave?" but rather "what's the idea behind this behaviour"? I'm pretty sure I'm missing something here.Layfield
yes, that's why I said "borderline". I personally think this is asking a completely different questionBastardize
Related info: in C++ the extern inline is not required (and in fact not permitted); the implementation must put one instance of the function somewhere (as opposed to C99 where you must specify exactly where the one instance is)Adjectival
One does not need to specify "inline" in the corresponding "extern" function declaration in the source code (.c) file. In other words, extern void f(); also works. (or just void f(); where extern will be implied). In fact, you can even have two "extern" declarations in the same source code (.c) file - an "extern inline" function declaration and a non-inline "extern" function declaration! But any declarations for the inline function in other source code files must all be bare inline function declarations (where the "extern" linkage is not specified or implied). AVOID! Keep it simple!Depressant
In C, should inline functions in headers be externed in the .c file? has an answer that shows how to actually declare the extern inline version in exactly one .c, like the question here does. (But none of the answers!)Kyrakyriako
WRT "And there is indeed no use of inline in a .c file -- it's only useful in headers" I assume you mean: no use for inline in a .c file without extern or staticBawl
P
53

Actually this excellent answer also answers your question, I think:

What does extern inline do?

The idea is that "inline" can be used in a header file, and then "extern inline" in a .c file. "extern inline" is just how you instruct the compiler which object file should contain the (externally visible) generated code.

[update, to elaborate]

I do not think there is any use for "inline" (without "static" or "extern") in a .c file. But in a header file it makes sense, and it requires a corresponding "extern inline" declaration in some .c file to actually generate the stand-alone code.

Prenomen answered 10/6, 2011 at 22:48 Comment(9)
Yeah, I never understood this myself until just now, so thanks for asking :-). It is weird in that the non-extern "inline" definition goes in the header (but does not necessarily result in any code generation at all), while the "extern inline" declaration goes in the .c file and actually causes the code to be generated.Prenomen
Does this mean I write inline void f() {} in the header and extern inline void f(); in the .c file? So the actual function definition goes in the header and the .c file contains a mere declaration in this case, in reversal of the usual order?Layfield
Is it possible to write an inline function that is compatible with both C99 and C++ definitions? (bear with me, i'm still reading the other answers)Franctireur
Well, it looks like that a C99 inline function (with an “extern inline” code-generation trigger in a .c file) will work fine in C++. It will just look silly and redundant to a C++ developer. An extern declaration that generates code? What was the C99 Committee thinking? Reading the rationale, it seems that you can omit both inline and extern to get the same result as the “extern inline” code-generation trigger, and I think it would look less stupid that way, because the original meaning of the “extern” keyword is “don't {generate code|allocate space} here”.Franctireur
@endolith: I do not understand your question. We are discussing inline without static or extern. Of course static inline is fine, but that is not what this question and answer are about.Prenomen
I understand how to make it work; what I don't find in this answer is, why the code in OP's question can't work. The main() and the f() are definitely in the same translation unit, so main() can see f() in the first place. I still don't understand why the "static" keyword can make a difference.Vanmeter
@KunWu: The inline f() definition does not actually emit any code, so it results in an undefined symbol at link time. The static keyword causes the definition to emit (locally visible) code. These are the semantics... An optimizing compiler might actually expand the code inline in either case, or even without the inline keyword. In C, the inline keyword has more to do with scoping and linkage rules than with actual inlining.Prenomen
"The idea is that "inline" can be used in a header file, and then "extern inline" in a .c file." => Don't you mean the other way around? At least my gcc complains with duplicate symbols at link time if I use inline without extern in a .h file.Ailurophile
@MatthieuMoy You need to use -std=c99 instead of -std=gnu89.Gebhardt
C
32

From the standard (ISO/IEC 9899:1999) itself:

Appendix J.2 Undefined Behaviour

  • ...
  • A function with external linkage is declared with an inline function specifier, but is not also defined in the same translation unit (6.7.4).
  • ...

The C99 Committee wrote a Rationale, and it says:

6.7.4 Function specifiers

A new feature of C99: The inline keyword, adapted from C++, is a function-specifier that can be used only in function declarations. It is useful for program optimizations that require the definition of a function to be visible at the site of a call. (Note that the Standard does not attempt to specify the nature of these optimizations.)

Visibility is assured if the function has internal linkage, or if it has external linkage and the call is in the same translation unit as the external definition. In these cases, the presence of the inline keyword in a declaration or definition of the function has no effect beyond indicating a preference that calls of that function should be optimized in preference to calls of other functions declared without the inline keyword.

Visibility is a problem for a call of a function with external linkage where the call is in a different translation unit from the function’s definition. In this case, the inline keyword allows the translation unit containing the call to also contain a local, or inline, definition of the function.

A program can contain a translation unit with an external definition, a translation unit with an inline definition, and a translation unit with a declaration but no definition for a function. Calls in the latter translation unit will use the external definition as usual.

An inline definition of a function is considered to be a different definition than the external definition. If a call to some function func with external linkage occurs where an inline definition is visible, the behavior is the same as if the call were made to another function, say __func, with internal linkage. A conforming program must not depend on which function is called. This is the inline model in the Standard.

A conforming program must not rely on the implementation using the inline definition, nor may it rely on the implementation using the external definition. The address of a function is always the address corresponding to the external definition, but when this address is used to call the function, the inline definition might be used. Therefore, the following example might not behave as expected.

inline const char *saddr(void)
{
    static const char name[] = "saddr";
    return name;
}
int compare_name(void)
{
    return saddr() == saddr(); // unspecified behavior
}

Since the implementation might use the inline definition for one of the calls to saddr and use the external definition for the other, the equality operation is not guaranteed to evaluate to 1 (true). This shows that static objects defined within the inline definition are distinct from their corresponding object in the external definition. This motivated the constraint against even defining a non-const object of this type.

Inlining was added to the Standard in such a way that it can be implemented with existing linker technology, and a subset of C99 inlining is compatible with C++. This was achieved by requiring that exactly one translation unit containing the definition of an inline function be specified as the one that provides the external definition for the function. Because that specification consists simply of a declaration that either lacks the inline keyword, or contains both inline and extern, it will also be accepted by a C++ translator.

Inlining in C99 does extend the C++ specification in two ways. First, if a function is declared inline in one translation unit, it need not be declared inline in every other translation unit. This allows, for example, a library function that is to be inlined within the library but available only through an external definition elsewhere. The alternative of using a wrapper function for the external function requires an additional name; and it may also adversely impact performance if a translator does not actually do inline substitution.

Second, the requirement that all definitions of an inline function be “exactly the same” is replaced by the requirement that the behavior of the program should not depend on whether a call is implemented with a visible inline definition, or the external definition, of a function. This allows an inline definition to be specialized for its use within a particular translation unit. For example, the external definition of a library function might include some argument validation that is not needed for calls made from other functions in the same library. These extensions do offer some advantages; and programmers who are concerned about compatibility can simply abide by the stricter C++ rules.

Note that it is not appropriate for implementations to provide inline definitions of standard library functions in the standard headers because this can break some legacy code that redeclares standard library functions after including their headers. The inline keyword is intended only to provide users with a portable way to suggest inlining of functions. Because the standard headers need not be portable, implementations have other options along the lines of:

#define abs(x) __builtin_abs(x)

or other non-portable mechanisms for inlining standard library functions.

Cavazos answered 10/6, 2011 at 22:57 Comment(0)
N
1

> I get a linker error (undefined reference to f)

Works here: Linux x86-64, GCC 4.1.2. May be a bug in your compiler; I don't see anything in the cited paragraph from the standard that forbids the given program. Note the use of if rather than iff.

An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit.

So, if you know the behavior of the function f and you want to call it in a tight loop, you may copy-paste its definition into a module to prevent function calls; or, you may provide a definition that, for the purposes of the current module, is equivalent (but skips input validation, or whatever optimization you can imagine). The compiler writer, however, has the option of optimizing for program size instead.

Neoteny answered 10/6, 2011 at 22:57 Comment(1)
The standard states that the compiler may always assume that there is an external function with the same name and call it instead of the inline version, so I don't think it's a compiler bug. I tried with gcc 4.3, 4.4 and 4.5, all give a linker error.Layfield

© 2022 - 2025 — McMap. All rights reserved.