cc optimize mode

Discussion in 'C Programming' started by WG, Mar 30, 2012.

  1. WG

    WG Guest

    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
    WG, Mar 30, 2012
    #1
    1. Advertising

  2. WG

    Jorgen Grahn Guest

    On Fri, 2012-03-30, WG wrote:
    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 30, 2012
    #2
    1. Advertising

  3. WG

    Ike Naar Guest

    On 2012-03-30, WG <> wrote:
    > 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 */
    Ike Naar, Mar 30, 2012
    #3
  4. WG

    WG Guest

    Thanks for your tipps, helped me a lot!

    WG
    WG, Mar 31, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J Lee
    Replies:
    3
    Views:
    470
    bruno at modulix
    Dec 1, 2005
  2. Edward Loper
    Replies:
    0
    Views:
    458
    Edward Loper
    Aug 7, 2007
  3. John J Lee
    Replies:
    0
    Views:
    513
    John J Lee
    Aug 7, 2007
  4. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    425
    Edward Loper
    Aug 9, 2007
  5. Neal Becker
    Replies:
    0
    Views:
    378
    Neal Becker
    Aug 9, 2007
Loading...

Share This Page