>> Carl Lowenstein wrote:
>If the only use of the function is to keep the string from being
>optimized away, what keeps the function from also being optimized
>away?
Nothing, really. I deal with a made-for-embedded-micro's C compiler all the
time. Because it outputs a listing file, I get to see exactly what it does
with my code. There have been several tweak-and-check-the-result sessions
dealing with this.
In a case similar to yours, I had a bitmap defined that I wasn't using. I
didn't want to see the warning about that; the fix was as simple as placing
this in the startup section of main():
i = bitmap[0][0];
<soapbox warning="not gcc, ymmv">
Making stuff compile without warnings is good practice. Although some of it
can be a waste of time (even I refuse to cast every literal '.' to a char),
it can serve as a learning experience and often leads to things about C or
its internals that I didn't know.
If you define a datum used by a function called by a function that's called
by a function that's never used, none of it gets compiled. Though your
question was probably rhetorical. I can get the effect of not #include'ing
whole modules, just by commenting out the call to their root function.
So here's another one. For convenience I sometimes define a breakpoint
function so I can have it in one place. If I call it once from the program,
it gets compiled in line. I have to call it twice for it to be made into a
called/returned function.
And more along the lines of Carl's problem. If the solution he seeks
requires there to be a function (I know it doesn't), then to get it included
you just have to convince the compiler it COULD happen even if it never does:
if( 0==1 ) idstring_init();
Well, except of course the compiler figures out THAT will never be true and
tosses the whole thing, so:
int blah=0;
if(blah) idstring_init();
I have to do this, in fact, because for some reason a compiler intended for
EMBEDDED micro's thinks that:
while(1) {
task1();
task2();
}
is a loop that can never terminate, and warns me so; which is logical,
except that most embedded apps HAVE to do this in some way! So I have to
disguise the "1".
Turns out, I can suppress warnings (hide the errors :-O ) but it's not
pretty having to sprinkle #errors off and #errors on around my code.
Similar to needing "__attribute__((used))"...
Hey, at least this thing _talks_ to me...
</soapbox>
Barry
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg