How can I force GCC to compile functions that are not used?

Being a pragmatist, I would just put:

// Hopefully not a name collision :-)
void *xyzzy_plugh_zorkmid_3141592653589_2718281828459[] = {
    &functionToForceIn,
    &anotherFunction
};

at the file level of one of your source files (or even a brand new source file, something along the lines of forcedCompiledFunctions.c, so that it's obvious what it's for).

Because this is non-static, the compiler won't be able to take a chance that you won't need it elsewhere, so should compile it in.


Be careful as the -Wunused-functions doesn't warn of unused functions as stated above. It warns of ununsed STATIC functions.

Here's what the man page for gcc says:

-Wunused-function Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall.

This would have been more appropriate as a comment but I can't comment on answers yet.


You could try __attribute__ ((used)) - see Declaring Attributes of Functions in the gcc manual.


Your question lacks a few details but I'll give it a shot...

GCC generally removes functions in very few cases:

  • If they are declared static
  • In some cases (like when using -fno-implement-inlines) if they are declared inline
  • Any others I missed

I suggest using 'nm' to see what symbols are actually exported in the resulting .o files to verify this is actually the issue, and then see about any stray 'static' keywords. Not necessarily in this order...

EDIT:

BTW, with the -Wall or -Wunused-function options GCC will warn about unused functions, which will then be prime targets for removal when optimising. Watch out for

warning: ‘xxx’ defined but not used

in your compile logs.