인라인 함수에 대한 'Undefined external'메시지 처리

기술노트 49981

아키텍처:

All

컴포넌트:

compiler

업데이트:

2018-02-28 오전 1:48

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.

죄송하지만, 당사 사이트에서는 Internet Explorer를 지원하지 않습니다.보다 편안한 사이트를 위해 Chrome, Edge, Firefox 등과 같은 최신 브라우저를 사용해 주시길 부탁드립니다.