How to force linker to link unused const global variables?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to force linker to link unused const global variables?

Jump to solution
4,717 Views
wangyanjun
Contributor II
Thanks !
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,805 Views
CompilerGuru
NXP Employee
NXP Employee
list them in a ENTRIES Var0 Var1 END block in the prm file. Check the linker manual.

View solution in original post

0 Kudos
Reply
6 Replies
1,804 Views
wangyanjun
Contributor II
Is there any more simpler method? I have a lot of calibrated data to be treated as that. This method is too tedious. Thanks!
0 Kudos
Reply
1,805 Views
Lundin
Senior Contributor IV
The suggested method is good, because you won't have to alter the source code.

The alternative is less appealing:

/* unused variables: */
#pragam CONST_SEG GLOBAL_CONST
const int x = 123;
const int y = 456;
...
#pragma CONST_SEG DEFAULT


int main()
{
list_unused variables();
}


#pragma MESSAGE DISABLE C4002 /* result not used */
void list_unused variables()
{
(void) x;
(void) y;
...
}
#pragma MESSAGE DEFAULT C4002
0 Kudos
Reply
1,805 Views
CompilerGuru
NXP Employee
NXP Employee
That actually wont even help as the compiler has no reason to use x/y in list_unused variables, they are not volatile. So this version would need a few volatile's too, and would cause unnecessary code to be generated so its not the right thing to do anyway.

I would still propose to list the variables to be used. As alternative, the manual also specifies how to use ENTRIES with elf object file names and not just with individual objects. Details: In the linker manual part of
Help\PDF\Build_Tools_Utilities.pdf.

Daniel
0 Kudos
Reply
1,805 Views
Lundin
Senior Contributor IV
It does help.

(void) x;

translates to

0004 fc0000 LDD x

On the HCS12 compiler.
0 Kudos
Reply
1,805 Views
CompilerGuru
NXP Employee
NXP Employee
Well, I would not rely on that optimization are not taking place.
For me, default options (say no options), CW For HC12 V4.6:

Code:
Function: list_unused_variables   18:  (void) x;   19:  (void) y;   20:  }  0000 3d           [5]     RTS  

Anyway, its not the right thing to do.

Daniel

0 Kudos
Reply
1,806 Views
CompilerGuru
NXP Employee
NXP Employee
list them in a ENTRIES Var0 Var1 END block in the prm file. Check the linker manual.
0 Kudos
Reply