EmbDev.net

Forum: ARM programming with GCC/GNU tools Unwanted remove of unused variables with GCC


Author: Mattias (Guest)
Posted on:

Rate this post
0 useful
not useful
Hi,

I have a application where I need to declare a couple of variables in 
RAM but they will not be used in the application. I then use the RAM 
addresses for the variables, to change there values via a communication 
port.

Unfortunately GCC remove the variables if I don't use them, and I don't 
want to add "fake use" like "if(var_name){}"

I don't use any optimizing at the moment, the variables are removed 
anyway.

Is there a way to keep them?

Thanks

/Mattias

Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Your fake use wouldn't help anyway, as it will be optimized away
as well.

Not sure how a "communication port" could change SRAM data without
the intervention of code, anyway, what you're probably looking for
is
__attribute__((used))

Author: Johann L. (gjlayde)
Posted on:

Rate this post
0 useful
not useful
I don't quite understand what you are doing... you use the address of a 
variable the variable is used (in general) and the object must not be 
canceled out. Can you give a small example in C to make it clear?

Jörg Wunsch wrote:
> Not sure how a "communication port" could change SRAM data without
> the intervention of code, anyway, what you're probably looking for
> is
>
>
__attribute__((used))

AFAIR this helps for functions, not for vars.

Author: Andreas B. (Guest)
Posted on:

Rate this post
0 useful
not useful
The attribute does help for variables:

> `used'
>      This attribute, attached to a variable, means that the variable
>      must be emitted even if it appears that the variable is not
>      referenced.

Author: Johann L. (gjlayde)
Posted on:

Rate this post
0 useful
not useful
Andreas B. wrote:
> The attribute does help for variables:
>
>> `used'
>>      This attribute, attached to a variable, means that the variable
>>      must be emitted even if it appears that the variable is not
>>      referenced.

Which version of gcc does this refer to?

Author: Andreas B. (Guest)
Posted on:

Rate this post
0 useful
not useful
Ah, got me there. I just looked in the current docs I had. Looking 
through the docs on gcc.gnu.org, it appears the 'used' attribute for 
variables appeared in version 4.2.

Author: Philipp Burch (philipp_burch)
Posted on:

Rate this post
0 useful
not useful
Another way to ensure that the variables exist could be to declare them 
as volatile. Since they could be changed by hardware (for whatever 
reason) this should be the appropriate method. The problem sounds a bit 
strange to me, however.

Author: Big Wall (tylr2005)
Posted on:

Rate this post
0 useful
not useful
Hi
Have you try the "volatile" keyword?

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.