[cfe-dev] Pointers to inline functions

Justin Bogner mail at justinbogner.com
Wed Oct 16 16:30:05 PDT 2013


The following program fails to link when built with -O0:

    inline void func(void) { }
    
    int main(int argc, const char *argv[]) {
      void (*f)(void) = func;
      f();
      return 0;
    }

This is because `func` has GVA_C99Inline linkage, so we decide that an
external definition is available and we don't need to emit the inline
definition. Since we use `func` through a function pointer, we end up
referencing and undefined symbol.

Is this correct? The C99 and C11 standards aren't very clear on whether
or not this is allowed. The relevant paragraph is n1570 6.7.4, p7:

    Any function with internal linkage can be an inline function. 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. 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.

I think this says that a separate extern definition is needed, but also
that it's acceptable to call the inline function here.



More information about the cfe-dev mailing list