LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Statically linking only used functions

[ILUG] Statically linking only used functions

Brian Foster blf at blf.utvinternet.co.uk
Fri Sep 10 19:20:54 IST 2004


  | Date: Fri, 10 Sep 2004 11:52:44 +0100
  | From: Bryan O'Donoghue <typedef at eircom.net>
  | 
  | Library:  libblah.a
  |  void do_something();
  |  void not_used();
  | 
  | Proggie:  proggie_blah
  |  #include <blah.h>
  |  int main(int argc, char argv[]){
  |     do_something();
  |     return 0;
  |  }
  | 
  | gcc -o proggie_blah /usr/lib/libblah.a proggie_blah.c
  | 
  | Is there anyway in the static link case above, to only link
  | do_something() and not link not_used(), am I reaching here?

 whilst yer posing is a tad unclear, I am assuming:

  - do_something() and not_used() are both in libblah.a

  - neither references the other.

  - the source to do_something() and not_used() are both
     in the same C (.c) file; or more accurately, the code
     for both is in the same object (.o) file in libblah.a.

 in which case, the fix is to break apart do_something() 
 and not_used() into separate files; i.e., arrange it so
 they are in separate objects in the library.   done.

 the linker loads object modules (.o files), not functions
 or data.  it _finds_ which .o's to link by what symbols
 they define, but you then get the entire object module.

 so if you do not want this, the solution is to separate
 the functions into separate source files.  which of course
 assumes you have, can play with, and are willing to play
 with, the source to the offending libraries;  and that
 those libraries are sufficiently modular internally to
 allow separation of functions into individual files.

 there are such things as "optimising linkers" which do
 extract only the referenced bits from object modules.
 ( actually, what they do  --- or at least the one I used
  to use did ---  was still load the entire module, and
  then in a separate second pass after the program had
  been fully linked, remove all blocks (functions/data)
  which were not referenced by some other block.  repeat,
  again and again, until every block was referenced by
  some other block. )
 but I do not know of any such tools for/on Linux on any
 processor?   but I also have not looked ....

 there is no(?) link-time advantage to putting functions
 into individual files for a shared library, which is,
 IMHO, why this seems to be a dying art/skill/science?

cheers!
	-blf-
-- 
«How many surrealists does it take to    |  Brian Foster      Montpellier,
 change a lightbulb?  Three.  One calms  |  blf at utvinternet.ie      FRANCE
 the warthog, and two fill the bathtub   |    Stop E$$o (ExxonMobile)!
 with brightly-colored machine tools.»   |        http://www.stopesso.com



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell