This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: Static Global Variable optimized out by compiler/linker

Tool/software: TI C/C++ Compiler

Hello,

I have an initialized static const global variable which is NOT used anywhere in the code but i would like to keep (dont want it to be optimized out but linker/compiler).

I have added the used attribute and the retain pragma but it still does not work. i also tried adding --retain in the linker command file (to see if there is a problem with the pragma) but it did not help either.

I am using TDA2xx , the M4 core , compiler version: Ti.16.9.2.LTS

[update]: the --keep_unneeded_statics compiler option seem to help but the problem is that this option is a global one and not for a specific variable and i want only a specific variable to be kept

Can you please help - why does this work?

Thanks

Guy

  • Please used #pragma RETAIN instead.  Look that up in the ARM compiler manual.  In a future release, you will be able to use __attribute__((retain)).

    Thanks and regards,

    -George

  • Hi,
    maybe you misunderstood my message - i DID used pragma retain , when i did not work i tried --retain in the linker file which did not work either.
    i have also placed the used attribute on the variable but it also did not help.
    basically nothing worked for global static variable except if i enable the --keep_unneeded_statics flag which is a global one and i want to address only specific variable.

    it seems the problem is with the compiler and the variable never reaches the linker stage but that is what the used attribute is for - to tell the compiler it should keep the variable - from some reason it does NOT work

    any advice?
  • I'm sorry I overlooked that.

    As a workaround, please try what this customer did.  

    At the same time, this seems like a real problem in the compiler.  So, I'd appreciate a test case.  For the C source file where you use #pragma RETAIN to no effect, please submit a test case as described in the article How to Submit a Compiler Test Case.

    Thanks and regards,

    -George

  • Hi,
    What the link you referred to suggested is exactly what i have already done - which did not work.
    the only difference i can see is that my variable is a static one where in the link it is not.
    from some reason, for static variables the compiler does not respect the used attribute and if the static is not used it is thrown by the compiler (never even reaches the linker stage) unless the global flag to keep unused statics is set (which rather NOT set it globally)

    currently the only way for me was to add a "dummy" instruction inside some function that reads the variables so that the compiler wont throw it away but this is not really something i would like to have if i can avoid it ...

    Guy
  • I am able to construct a small test case which exhibits similar behavior.  I filed CODEGEN-5119 in the SDOWP system to have this investigated.  You are welcome to follow it with the SDOWP link below in my signature.

    I am concerned that my test case may not match the details of your case.  One or more of those details could have a role in the problem behavior.  That is why I still would like to have the test case I requested earlier.

    Thanks and regards,

    -George

  • Hi,

    Below is an example to what i have (nothing much really)

    #pragma RETAIN(testStr)
    __attribute__((used)) static const char testStr[]="TESTING 123";

    main()

    {

    ...

    }

    The global static variable (testStr) is NOT referenced anywhere in the code !

    Also the fix (when there is one) should also apply with and without compiler optimization...

    Thanks

    Guy