12

I am splitting off some of the code in my project into a separate library to be reused in another application. This new library has various functions defined but not implemented, and both my current project and the other application will implement their own versions of these functions.

I implemented these functions in my original project, but they are not called anywhere inside it. They are only called by this new library. As a result, the compiler optimizes them away, and I get linking failures. When I add a dummy call to these functions, the linking failures disappear.

Is there any way to tell GCC to compile these functions even if they're not being called?

I am compiling with gcc 4.2.2 using -O2 on SuSE linux (x86-64_linux_2.6.5_ImageSLES9SP3-3).

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • @jcomeau: But what if you want them optimized? – Cascabel Nov 15 '10 at 07:49
  • if it works without optimization, then the thing to do is find out which specific optimizations to turn off; possibly -fno-inline-small-functions would be one, but I've never dug very deep into how gcc does its magic – jcomeau_ictx Nov 15 '10 at 07:54

4 Answers4

20

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

Paul R
  • 208,748
  • 37
  • 389
  • 560
4

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

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.

thkala
  • 84,049
  • 23
  • 157
  • 201
1

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.