4

I am using some functions of the GSL library in my C application. This library is so large and has a lot of functions and facilities which I want only a few of them. My problem is that I want to use my application on a small embedded system and using this large library makes my compiled application so large.

Is there any facility to extract a particular subset of functions and files from such large libraries?

Thank you all in advance,

Saba Jamalian
  • 750
  • 2
  • 10
  • 24
  • 6
    If you only need to use it in one application, you can statically link it (perhaps you are already doing that, given the comment about making the application itself large) and use toolchain options to strip unneeded code. – Chris Stratton Dec 15 '12 at 18:40
  • If you're using gcc and if you've access to the libraries source code, you might try to enable link-time-optimization. This essentially drops all code that isn't needed and also performs better inlining. Combine with -Os for better results. – fuz Dec 15 '12 at 23:02
  • try including just the headers you need, and not every gsl header and do as Chris Stratton says – pyCthon Dec 23 '12 at 14:48

3 Answers3

6

Is there any facility to extract a particular subset of functions and files from such large libraries?

Yes - that is what the linker does already.

A library is a collection if individual object code files. The linker will link only those object files necessary to actually to resolve references in your code. So long as the library designer made the library sufficiently granular (ideally one public function per object file), then your linked application code will not be related at all to the size of the library, only the sum of the code you have explicitly referenced.

So my advice is to build the code first then see if you actually have a problem - the chances are that you don't. Remember that your application probably already links the standard C library, and that in itself does not make your application the size of the entire library!

The linker will be able to output a MAP file that will detail exactly what object code is linked and the functions and data objects within them. You will be able to determine how efficient the link is - remember though that functions you call are likley to call other functions within the library that you have not explicitly referenced yourself. Your linker may also be able to output a cross-reference table that details such dependencies.

Clifford
  • 88,407
  • 13
  • 85
  • 165
2

You can actually use mklibs: https://launchpad.net/mklibs

It is able to strip the unused portions of your shared library, based on a set of bexecutable depending on that library.

You don't even have to have the code of your library.

Alexandre Belloni
  • 2,244
  • 13
  • 12
0

If you could modify the GLS library you could try to make a slim version.

The best way is to generate caller/callee graph using tool like doxygen and than pick only things that are used and needed. Probably you may also ignore some of the dependencies (e.g. logging, managing state, options that you don't need).

However, this approach will require significant amount of work. But if you are on a small embedded system chances are that the "slim version" will have not that many lines of code, so cherry-picking might be reasonable.

Jakozaur
  • 1,957
  • 3
  • 18
  • 20