Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching off optimization for a specific function in gcc 4.2.2

Tags:

c

gcc

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 work. We know which functions it is, but we have no clue of the code itself so the easiest and safest way would probably be to just switch it off for the whole function.

Sadly http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html shows that there is a an optimize function attribute / pragma, but it requires gcc 4.4, which we do not have.

thanks in advance

like image 638
Ronny Brendel Avatar asked Dec 27 '08 16:12

Ronny Brendel


2 Answers

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.

like image 189
frankodwyer Avatar answered Oct 05 '22 03:10

frankodwyer


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.

like image 30
Robert Gamble Avatar answered Oct 05 '22 03:10

Robert Gamble