Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating libraries

Status
Not open for further replies.

pairajesh

ISP
Jan 23, 2002
1
0
0
IN
I was of the opinion that if we put functions in library and then link them into program only that functions gets linked into the executable.
But what we found is that all the function are getting linked into the executable even if one function from the library is used.[ When we build a lib with only one function and then use it in the program the exe size is very small, but if we link it with a library having more functions and we are using only one from it the exe size is very large].

Is there any wrong we are making in compiling library or executable.??

 
It depends on how the functions are distributed. If you have one function per object file that you link into the library, then the executable will only contain that function and its dependencies. If, however, you have say a dozen functions in the object file that you linked into that library, the executable will pull in all 12 functions. It will also pull in all the dependencies that those 12 functions have and their dependencies ad nauseum.

If the functions within the library have dependencies, then make sure you put the high level ones first and the low level ones last otherwise you may get linking problems.

Some linkers only do one pass through the library. If you have

a.o b.o c.o

and c.o needs something in a.o but nothing in a.o was called from anywhere else, then you may get a link error. The way around it is to put the library two or more times on the link line.
 
If you're linking statically then the executable size will vary according to the size of the added library - the entire library is always added to the executable, although optimisation can and usually will find ways to snip out unused code if turned on, provided that the remaining code can be relocated. Are you using the correct compiler optimisations, if this is what you're doing?

If you're linking dynamically then the executable size will vary very little with regard to libraries; it will only contain library hints and symbol names.

If you're simply building an object file which contains your 'library functions' and linking that at compile time rather than actually forming a true independent library file, then the compiler will be free to do yet more optimisation in size; the absolute best that most compilers can do for optimisation reductions comes from compiling only object files, and preferably as few as possible of those. Single large object files are more open to optimsation than several smaller object files are. Interestingly the overall compilation time comes down quite well too. There's a nice little article on the subject from a few years back here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top