This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

inline function giving error in IAR

Hi,

I have implemented a small code to check the inline function in C. But it give the "Function undefined error " during linking stage. If I removed the inline in the function declaration than there is no error but it is not inlined properly, means the function is not copied in main instead it is calling the function. Have anyone tried as like this?

inline int add(int, int);

void main()
{
    int a = 10, b = 4, c = 0;
    c = add (a, b);
}

inline int add(int j, int k)
{
    int l = 0;
    l = j + k;
    return l;
}

  • Section 6.7.4 § 6 of the C99 standard says:

    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.

    In this case, the compiler chose to use the external definition, but you have never defined such a function.

    You should make this function one with internal linkage, i.e., you should define it as static. It would also be a good idea to define it before it is used.

  • In C, the keyword "inline" behaves a little bit different from how it's done in C++.

    In C++, a user could simply declare a function as "inline". In each file where the compiler decided it should not inline the function, it would generate a normal non-inlined version of it. The linker would then pick one of the compiled versions and use it in the final application.

    When "inline" was introduced in C, the standard committee didn't want to put this responsibility on the linker, as many development environments came with linker not capable of this. Instead, they decided that one and only one compilation unit should provide the non-inlined version. This is done by declaring it "extern" in that compilation unit. (Personally, I feel this is very backwards, as "extern" traditionally was only used to declare function and variables.)

    Having said that, the IAR tools allows a user to use "inline" in C as it works in C++ by either using "#pragma inline" (or even #pragma inline=forced") or by specifying the command line option --use_c++_inline (In Embedded Workbench: Options -> C/C++ Compiler -> Language 1 -> C dialect -> C++ inline semantics).

        -- Anders Lindgren, IAR Systems, Author of the IAR compiler for MSP430

  • In C++, a user could simply declare a function as "inline". In each file where the compiler decided it should not inline the function, it would generate a normal non-inlined version of it. The linker would then pick one of the compiled versions and use it in the final application.

    Wouldn't that not (usually) end up in "Multiple definitions of <not-really-inlined-function>" linker errors, if the compiler decides it not worth inlining anywhere ?

  • Wouldn't that not (usually) end up in "Multiple definitions of <not-really-inlined-function>" linker errors?

    No, the compiler/linker combination used for C++ is required to be able to handle multiple (identical) copies of such a function.
    (There is no such requirement for C.)

  • No, the compiler/linker combination used for C++ is required to be able to handle multiple (identical) copies of such a function.

    I should have said that I mean C, not C++.

    And, I'm not quite sure if this statement is true. I know the C++ compiler can handle multiple versions of (on source file level) identical function names, by adding a parameter-dependent signature. How should that work for identical functions, which would evaluate to identical signatures ? Guess that's the reason why I've seem mostly "static inline" definitions in C++ sources.

    I'm rarely using C++, so my knowledge is somehow superficial here ...

**Attention** This is a public forum