how to keep/initialize unused variables/constants

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

how to keep/initialize unused variables/constants

Jump to solution
4,136 Views
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 Solution
3,325 Views
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!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

3 Replies
3,326 Views
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,325 Views
adamrakam
Contributor III

Thank you !

3,325 Views
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 Kudos