VNL
VNL

Reputation: 31

Static library symbols missing in linked executable

I am trying to link a statically created .a library with another piece of C code.

However, in the final executable several symbols (function names) are are found missing when seen with the nm command. This is due to the fact that the linker (gcc being called) is stripping the symbols which are not referenced in the other piece 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 omitted this way?

Upvotes: 3

Views: 4744

Answers (3)

Sebastiaan M
Sebastiaan M

Reputation: 5885

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.

Upvotes: 1

t0mm13b
t0mm13b

Reputation: 34592

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

Upvotes: 1

Matthew Herrmann
Matthew Herrmann

Reputation: 432

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.

Upvotes: 1

Related Questions