How to force linker to link unused const global variables?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How to force linker to link unused const global variables?

跳至解决方案
5,575 次查看
wangyanjun
Contributor II
Thanks !
标签 (1)
标记 (1)
0 项奖励
回复
1 解答
2,663 次查看
CompilerGuru
NXP Employee
NXP Employee
list them in a ENTRIES Var0 Var1 END block in the prm file. Check the linker manual.

在原帖中查看解决方案

0 项奖励
回复
6 回复数
2,662 次查看
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 项奖励
回复
2,663 次查看
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 项奖励
回复
2,663 次查看
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 项奖励
回复
2,663 次查看
Lundin
Senior Contributor IV
It does help.

(void) x;

translates to

0004 fc0000 LDD x

On the HCS12 compiler.
0 项奖励
回复
2,663 次查看
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 项奖励
回复
2,664 次查看
CompilerGuru
NXP Employee
NXP Employee
list them in a ENTRIES Var0 Var1 END block in the prm file. Check the linker manual.
0 项奖励
回复