Preventing GCC from optimizing out variable

OP #1489006
I'm using CodeSourcery GCC for ARM and have this problem where when I 
enable any level of optimization (1,2 or 3), this struct I have defined 
as const is being optimized out.  It's a block of information that I 
have deliberately put into a certain part of flash, so it's not accessed 
by it's struct name, but by address.  GCC insists on removing it unless 
I use -o0.  I have tried to fool it by assigning another variable this 
struct's address, but it doesn't work.  Is there an easy way to simply 
tell GCC not to optimize this away?
#1489362
Jerry Milner wrote:
> Darn, didn't work.  No warning or errors, it still discards the struct.

What is "it"? Compiler or linker? The attribute only tells the compiler 
that the variable is used, the linker can still toss it out. Check if 
the variable is in the object file.

I am not a GNU expert, but it would appear that the ld-option 
`--print-gc-sections' gives you a report about removed input sections.

Regards
Marcus
http://www.doulos.com/arm/
OP #1489941
I don't know if the linker or compiler discarded it, but after adding 
the _asm_ statement, it appears in the .map file and most importantly, 
the code now works with optimization on.  The struct in question 
contains strings that are displayed in a menu, so it's obvious when this 
struct is missing.

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.