Unwanted remove of unused variables with GCC

Guest #1923514
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
Moderator #1923610
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
1
__attribute__((used))
#1924543
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
>
>
1
__attribute__((used))

AFAIR this helps for functions, not for vars.
Guest #1924584
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.
Guest #1924869
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.
#1926757
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.

Reply

A question of your own? This thread is over 16 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.