Linker removes functions and variables or external not found

Technical Note 51348

Architectures:

All

Component:

linker

Updated:

2018/5/31 9:14

Introduction

If functions and variables are not refered to in the main() call-tree or in any interrupt call-tree the linker will remove them by default.

You can prevent this and force the linker to keep a certain variable or function by using the __root extension keyword.

__root char myvariable;

__root void myfunction(void)
{
   /* function code */
}

Other methods to keep the functions/variables are to use

  • IAR XLINK Linker -g option
  • IAR ILINK Linker --keep option
  • use the REQUIRE keyword in assembler

This behavior has the benefits:

  • Less use of RAM and ROM.
  • Reduced chance of "segment did not fit" errors.
  • Non-used "external" variables/functions are not searched for, which can lead to fewer errors.

The compiler also removes unused variables on absolute addresses.
This can lead to a problem when other modules refer to them.
It can be described with the below code.
Only the variable test1 will be kept in this compile unit.
test2, test3 and test4 are not kept since they are not accessed.
If you in some other C file use test2 as external, it will not be found by the linker.

__no_init int test1 @ 0x0004;
__no_init int test2 @ 0x0008;
__no_init int test3 @ 0x000C;
__no_init int test4 @ 0x0010;

void abc(void)
{
   test1 = 1;
}

One important reason for doing like this is that the device specific .h files creates a very large number of variables which could create segment overlap errors if included in more than one module.

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

很抱歉,该页面尚未翻译成中文。

如果您有任何问题,请联系我们的中国团队

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.