Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC - Enable compiler flags only on specific functions

Tags:

c

gcc

In a project I'm working on, there's a quadruple-nested for loop in a large file I'm trying to optimize I think would benefit from a compiler unroll with -funroll-all-loops. However, when I add this flag to the compiler, it unrolls the other loops the rest of the file and makes the overall program run more slowly. Is there a way (possibly via a #pragma) to apply compiler flags only to certain functions in the file instead of the entire file?

Thanks in advance.

like image 577
Marshall Clay Avatar asked Oct 17 '12 01:10

Marshall Clay


People also ask

Which gcc flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What is f flag in gcc?

'f' stands for 'flag'. -fpic # flag to set position independent code -fno-builtin # don't recognize build in functions ... The technical definition is that 'f' defines "Control the interface conventions used in code generation".

Does the order of compiler flags matter?

For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.

How do I add a flag to my compiler?

Open your project and then go Project > Build Options > Compiler Flags . You can tick boxes in the "Compiler Flags" tab, and you can write other options in the "Other Options" tab. Do one or the other, e.g. don't tick the "-std=c++98" box and also put "-std=c++11" in the Other Options.


1 Answers

The GCC function attribute optimize can be used to set an optimization option for a single function:

void foo(int bar) __attribute__((optimize ("unroll-all-loops")))
{
}
like image 147
caf Avatar answered Sep 30 '22 08:09

caf