How can I tell gcc not to inline a function? How can I tell gcc not to inline a function? c c

How can I tell gcc not to inline a function?


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 __attribute__ ((noinline)) foo() {  ...}


GCC has a switch called

-fno-inline-small-functions

So use that when invoking gcc. But the side effect is that all other small functions are also non-inlined.


I know the question is about GCC, but I thought it might be useful tohave some information about compilers other compilers as well.

GCC'snoinlinefunction attribute is pretty popular with other compilers as well. Itis supported by at least:

  • Clang (check with __has_attribute(noinline))
  • Intel C/C++ Compiler (their documentation is terrible, but I'mcertain it works on 16.0+)
  • Oracle Solaris Studio back to at least 12.2
  • ARM C/C++ Compiler back to at least 4.1
  • IBM XL C/C++ back to at least 10.1
  • TI 8.0+ (or 7.3+ with --gcc, which will define __TI_GNU_ATTRIBUTE_SUPPORT__)

Additionally, MSVC supports__declspec(noinline)back to Visual Studio 7.1. Intel probably supports it too (they try tobe compatible with both GCC and MSVC), but I haven't bothered toverify that. The syntax is basically the same:

__declspec(noinline)static void foo(void) { }

PGI 10.2+ (and probably older) supports a noinline pragma whichapplies to the next function:

#pragma noinlinestatic void foo(void) { }

TI 6.0+ supports aFUNC_CANNOT_INLINEpragma which (annoyingly) works differently in C and C++. In C++, it's similar to PGI's:

#pragma FUNC_CANNOT_INLINE;static void foo(void) { }

In C, however, the function name is required:

#pragma FUNC_CANNOT_INLINE(foo);static void foo(void) { }

Cray 6.4+ (and possibly earlier) takes a similar approach, requiringthe function name:

#pragma _CRI inline_never foostatic void foo(void) { }

Oracle Developer Studio also supports a pragma which takes thefunction name, going back to at least Forte Developer6,but note that it needs to come after the declaration, even in recentversions:

static void foo(void);#pragma no_inline(foo)

Depending on how dedicated you are, you could create a macro thatwould work everywhere, but you would need to have the function name aswell as the declaration as arguments.

If, OTOH, you're okay with something that just works for most people,you can get away with something which is a little more aestheticallypleasing and doesn't require repeating yourself. That's the approachI've taken for Hedley, where thecurrent version ofHEDLEY_NEVER_INLINElooks like:

#if \  HEDLEY_GNUC_HAS_ATTRIBUTE(noinline,4,0,0) || \  HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \  HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \  HEDLEY_ARM_VERSION_CHECK(4,1,0) || \  HEDLEY_IBM_VERSION_CHECK(10,1,0) || \  HEDLEY_TI_VERSION_CHECK(8,0,0) || \  (HEDLEY_TI_VERSION_CHECK(7,3,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))#  define HEDLEY_NEVER_INLINE __attribute__((__noinline__))#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)#  define HEDLEY_NEVER_INLINE __declspec(noinline)#elif HEDLEY_PGI_VERSION_CHECK(10,2,0)#  define HEDLEY_NEVER_INLINE _Pragma("noinline")#elif HEDLEY_TI_VERSION_CHECK(6,0,0)#  define HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")#else#  define HEDLEY_NEVER_INLINE HEDLEY_INLINE#endif

If you don't want to use Hedley (it's a single public domain / CC0header) you can convert the version checking macros without too mucheffort, but more than I'm willing to put in ☺.