The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

gcc -- stop optimizing!

sirSolariussirSolarius Registered User regular
edited April 2007 in Help / Advice Forum
I have some code that I want to run on a microcontroller. One piece of the code basically says:

var = 0;
var = 1;
var = 0;

but it is important that these instructions get executed, because var maps to a signal getting sent to the real world, and var needs to stay high for a clock cycle and then go low again.

I'm scared that gcc will optimize this out when it comes time to compile the final version. Is there a keyword to prevent my statements from being touched?

sirSolarius on

Posts

  • His CorkinessHis Corkiness Registered User regular
    edited April 2007
    Use the volatile keyword when declaring the variable, eg

    volatile int var;

    His Corkiness on
  • cramsincramsin Registered User regular
    edited April 2007
    If you're using the compiler only for the micro recompile it without the optimizations. The switch should be listed in ./configure --help
    Not sure how to do it just for a file.

    cramsin on
    Poke my man! 1418 3225 5229
  • elmoelmo Registered User regular
    edited April 2007
    gcc -O <some number>
    might also do the trick. lower number = less optimizing, might be just a CPU thing tho.

    elmo on
  • MasterDebaterMasterDebater Registered User regular
    edited April 2007
    Use the volatile keyword when declaring the variable, eg

    volatile int var;

    I've never tested this but K&R says it should work.

    MasterDebater on
  • JaninJanin Registered User regular
    edited April 2007
    Use the volatile keyword when declaring the variable, eg

    volatile int var;

    This is the way to go.
    elmo wrote: »
    gcc -O <some number>
    might also do the trick. lower number = less optimizing, might be just a CPU thing tho.

    -O0 (no optimizations) is the default, there's no need to set an option for it.

    Janin on
    [SIGPIC][/SIGPIC]
Sign In or Register to comment.