tags:

views:

1967

answers:

5

Say I have this small function in a source file

static void foo()
{

}

and I build an optimized version of my binary yet I don't want this function inlined (for optimization purposes). is there a macro I can add in a source code to prevent the inlining?

thanks

+1  A: 

Use the noinline attribute:

int func(int arg) __attribute__((noinline))
{
}

You should probably use it both when you declare the function for external use and when you write the function.

Chris Lutz
+5  A: 

You want the gcc-specific noinline attribute.

This function attribute prevents a function from being considered for inlining. If the function does not have side-effects, there are optimizations other than inlining that causes function calls to be optimized away, although the function call is live. To keep such calls from being optimized away, put asm ("");

Use it like this:

void foo() __attribute__ ((noinline))
{
  ...
}
alex tingle
Using gcc 4.4.3 on Arch Linux, I get a syntax error with the attribute placed as above. It works correctly when it precedes the function (e.g., __attribute__ ((noinline)) void foo() {})
mrkj
+7  A: 

A portable way to do this is to call the function through a pointer:

void (*foo_ptr)() = foo;
foo_ptr();

Though this produces different instructions to branch, which may not be your goal. Which brings up a good point: what is your goal here?

Andy Ross
+1  A: 

In case you get a compiler error for "attribute((noinline))", you can just try:

noinline int func(int arg)
{
    ....
}
arsane
+1  A: 
static __attribute__ ((noinline))  void foo()
{

}

This is what worked for me.

KenBee