cc optimize mode

W

WG

Greetings,


when compiling a program with option -O (gcc on Linux) alle unused
variables are stripped.

However, I would like to keep a specific variable.
I use it for version checking in object files with
'strings file.o | grep Id:'.

I do not want to enter a dummy instruction using this variable to
prevent the optimizer from stripping the variable away.

Is this even possible when using the option -O?


Thanks for hints
WG
 
J

Jorgen Grahn

Greetings,


when compiling a program with option -O (gcc on Linux) alle unused
variables are stripped.

-O2 is much more useful than -O.
However, I would like to keep a specific variable.
I use it for version checking in object files with
'strings file.o | grep Id:'.

Better, use ident(1) from the RCS package.
I do not want to enter a dummy instruction using this variable to
prevent the optimizer from stripping the variable away.

Why not? It doesn't have to be something that's actually *called*.

const char* id(void) { return "$Id$"; }
Is this even possible when using the option -O?

Read the gcc manual to see if there are any gcc-specific extensions
for doing this. For functions, there's the 'used' attribute; don't
know about variables.

/Jorgen
 
I

Ike Naar

when compiling a program with option -O (gcc on Linux) alle unused
variables are stripped.

However, I would like to keep a specific variable.
I use it for version checking in object files with
'strings file.o | grep Id:'.

I do not want to enter a dummy instruction using this variable to
prevent the optimizer from stripping the variable away.

Is this even possible when using the option -O?

How is the variable defined?

If the variable has file scope and external linkage,
as below, gcc 4.1.2 -O will keep it:

/* begin id.c */

char const * const version_id = "Id:"; /* never used */

int main(void)
{
return 0;
}

/* end id.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top