Handling ‘Undefined external’ message for inline function

Technical Note 49981

Arkitekturer:

All

Komponent:

compiler

Uppdaterad:

2020-06-23 14:52

Introduction

Sometimes linker errors are issued for C source files with inlined functions. The error messages are:

  • Error[e46]: Undefined external ... (from XLINK)
  • Error[Li005]: no definition for ... (from ILINK)

This technical note discusses how to resolve this situation.

Discussion

The linker error is issued because the compiler cannot fulfill some basic conditions (for inlined functions). In other words, check the following points and change the C source accordingly:

  • The compiler must at compile-time be able to ‘see’ the definition of the inlined function.
  • If the inlined function is used in precisely one (1) .c file, move the definition to that .c file.
  • To use an inlined function in several .c files:

    1. Put the function definition in a .h file.
          // To be present in a .h file (for example foo.h), 
          // typically included by many .c files. 
          inline unsigned char foo(unsigned char c) 
          { 
             return (c+1); 
          }​
    2. Include that .h file in all .c files where there is a call to the function.
    3. Make sure that there is precisely one (1) .c file with an extern declaration of the inlined function.
      // To be present in precisely one (1) .c file
      // (for example foo.c)
      extern inline unsigned char foo(unsigned char c);​​​

The reasons for P.1 and P.2 are straightforward. But the reason for P.3 can be harder to understand. From the ISO standard for C: '...An inline definition does not provide an external definition for the function...' So the extern declaration (from P.3) is used (by the linker) to resolve any non-inlined references to the function.

Conclusion

There are constrains C source code that contains inlined functions. For example, make sure that the inlined function does not refer to any static symbols (neither variables nor functions).

For more information, see Inlining functions (in the Development Guide or in the Compiler Reference Guide).

All product names are trademarks or registered trademarks of their respective owners.

Det här innehållet finns tyvärr inte på svenska.

Vår webbplats finns främst på vårt koncernspråk engelska, förutom det innehåll för investerare som vi är lagstadgade att kommunicera på svenska. Vi rekommenderar att du besöker vår globala webbplats på engelska för att få en bättre upplevelse.

Vi stöder inte längre Internet Explorer. För att få bästa möjliga upplevelse av iar.com rekommenderar vi att du uppgraderar till en modern webbläsare som Chrome eller Edge.