how to keep/initialize unused variables/constants

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

how to keep/initialize unused variables/constants

跳至解决方案
4,784 次查看
adamrakam
Contributor III

Hi,

I want to keep unused constant in my code but I get strange initial value.

pastedImage_1.png

I use 3 variations but none of them initialize argument on left side by value 21 :

static              uint32_t prm_sw_version                __attribute__((used)) = 21;

static volatile  uint32_t prm_sw_version_volatile   __attribute__((used)) = 21;

static const    uint32_t prm_sw_version_const     __attribute__((used)) = 21;

Why are left side arguments not initialized ?

Thank you !

1 解答
3,973 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Adam,

I also experienced the same problem as yours when I developed my project.

In the past when I use KDS, I use KEEP command in ld file to avoid variable being optimized.

MCUXpresso uses manage linker script, if we modify ld file by hand, we will loose the convenience of "manage linker script".

To avoid optimizing global variable, sometimes I wrote dummy code. For example, to avoid g_var being optimized:

int g_var;
main(){  
volatile int dummy=0;  
dummy=g_var;
...
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

在原帖中查看解决方案

3 回复数
3,974 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Adam,

I also experienced the same problem as yours when I developed my project.

In the past when I use KDS, I use KEEP command in ld file to avoid variable being optimized.

MCUXpresso uses manage linker script, if we modify ld file by hand, we will loose the convenience of "manage linker script".

To avoid optimizing global variable, sometimes I wrote dummy code. For example, to avoid g_var being optimized:

int g_var;
main(){  
volatile int dummy=0;  
dummy=g_var;
...
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

3,973 次查看
adamrakam
Contributor III

Thank you !

3,973 次查看
lpcxpresso_supp
NXP Employee
NXP Employee

If nothing in your code is actually using those variables,  would suspect that they have actually been removed from your linked application (the used attribute doesn't actually do what you might expect in many cases).

Check the map file generated by the linker into the Debug (or Release) subdirectory of your project after doing a build to find out what has been discarded.

Regards,

MCUXpresso IDE Support

0 项奖励
回复