Where should function attributes go?

Viewed 852

Suppose I want to mark a non-inline function with [[gnu::cold]]; should the attribute be placed in the declaration in the header, or should it go with the definition in a source file? Assume that I will not be using LTO and simply want that specific function to be optimized for binary size and not execution speed.

header example:

[[gnu::cold]] void rarely_called_func();

source file example:

[[gnu::cold]] void rarely_called_func() { ... }

Also, which position in the declaration/definition should it be:

/* A */ int /* B */ func () /* C */;
1 Answers

Unless the attribute is seen by the compiler, it cannot use the attribute in its optimisation. If you don't put the attribute in the declaration, then the compiler cannot see the attribute. Conclusion: In order for the compiler to use the attribute for optimisation, you must put the attribute to the declaration of the function (in the header file).

Related