Is it possible to switch off optimization of a specific function? A friend of mine has the problem that the gcc optimization makes some (unknown to me) µ-controller-code not
Short of putting the function in its own file, I don't think there is any way to turn off optimization on a function by function level using earlier versions of GCC. But instead of switching off the optimization for this one function, you could try selecting switching off certain types of optimizations in the whole program. While you have identified a bug in this particular function, this likely points to the existence of other undiscovered bugs.
As others pointed out, it's likely that the culprit is optimizations regarding 'strict aliasing'. While in the long run you probably should fix the code in question, in the short run you could play with adding '-fno-strict-aliasing' to your command line. At -O2 and above, the compiler makes certain assumptions about interactions between pointers. Adding this option tells it not to make these assumptions.
If this fails, and if for some reason the code cannot be fixed, you could then try disabling other optimization options.
You could put the function in a separate file and compile that file without optimization but the better solution would be to figure out what is wrong with the code and fix it.
One of the most common bugs that appears when optimization is enabled with gcc is with strict aliasing. Make sure all warnings are enabled and see if you get any warnings that might help you figure out what the problem is. If you can't figure it out, try to reduce the problem to a small, complete program and post it here.
If the pragma won't work for you, try splitting the function into its own file, and then compile that file without the optimisation flag set.
The answers so far have neglected the key words in the original question, which are "microcontroller code" It is very common when writing such code to with to disable optimizations -- aggressive optimizers will "optimize away" whole statements whose side effect is to drive the controller. This is a different world from application coding. For an application in the usual space of programming, I came here looking for the same info in order to avoid having a routine for Kahan summation (see wikipedia) optimized into nothingness. So let's not assume that a change of optimization level generating different program behavior is automatically a sign of bad code. Somethings can be kludged by using the volatile keyword cleverly, and in some cases one should generate the actual assembly language and inspect it. (I believe this can still be done with the -S switch to gcc). Let's remember that C is intended to be a sort of portable assembler, not a kind of COBOL.
Dave
For people who are still using older version of gcc: Newer version of gcc solves this problem. I have successfully used this in gcc-4.5.1. Goodluck.
I know this is an old post. The GCC page that OP gives actually says: To keep such calls from being optimized away, put asm (""); in the function. I guess this is an easy workaround. Just fine out this, hope it can helper other people like me.