views:

269

answers:

3

Hi, I am trying to link a statically created .a library with another piece of C code. Butin the final executable several symbols (function names) are are found missing when seen with the nm command. Is itdue to the fact that the linker (gcc being called) is stripping the symbols which are not referenced in the other pirce of C code that is being linked with the library. The function symbol that I am trying to find with the nm command is visible in the .a library. How can I make the linker not strip the symbols ommitted this way ? Ragards, VNL

+1  A: 

Generally, the linker does strip out other symbols - mainly for

  • Reduce the final size of the executable
  • Speed up the execution of the program

There are two trains of thoughts here:

  • When you use the option -O as part of the gcc command line, that is optimizing the code and thus all debugging information gets stripped out, and hence the linker will automatically do the same.
  • When you use the option -g as part of the gcc command line, that includes all debugging information so that the executable can be loaded under the debugger with symbols intact.

In essence those two are mutually exclusive - you cannot have both combined.

So it depends on which switches did you use for this to happen. Usually, -g switch is for internal debugging and testing prior to public release. The opposite would be something like this -O2 which makes the compiler smart enough to generate a executable that would be considered optimized such as removing dead variables, unrolling loops and so on.

Hope this helps and gives you the hint, Best regards, Tom.

tommieb75
In fact, if I rightly recall, with Borland's compiler (think it was 3.1 and 5.5), it complains about using (was it?) printf's and linking with floating points because there's no nudge in the code to force the link to work and the workaround was to include a dummy function that references a float..can someone back me up on this?
tommieb75
+1  A: 

Normally you need to call some registration function in your application to generate such a reference. Of course if you don't have access to the code of the first library, you can only use the -g option as described by tommieb75.

Hope this helps.

Regards,

Sebastiaan

Sebastiaan Megens
A: 

Compile in gcc with -dynamic to force the compiler to include all symbols. But make sure that's what you really want, since it's wasteful.

Might be useful for some static factory patterns.

Matthew Herrmann