Preventing GCC from optimizing out variable

OP #1489006
Rate this post
useful
not useful
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
Rate this post
useful
not useful
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
Rate this post
useful
not useful
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

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now