42

I'm tired of adding ten link libraries into my project, or requiring eight of them to use my own. I'd like to take existing libraries like libpng.a, libz.a, libjpeg.a, and combine them into one single .a library. Is that possible? How about combining .lib libraries?

Community
  • 1
  • 1
GhassanPL
  • 2,594
  • 4
  • 30
  • 39

6 Answers6

26

You could extract the object files from each library with

ar x <library name>

and then merge them all into a new library with

ar cs <new library name> <list each extracted object file>
Judge Maygarden
  • 25,264
  • 8
  • 76
  • 98
  • This does not work on all ar. Reading the manual I found that earlier versions of ar it's easier to do: `sh4-linux-ar r *.o` to append other symbols to `existinglib.a` – Edu Felipe Aug 04 '10 at 20:32
  • This also would cause issues for command line length. Compiling .a/.lib files is often used to get around the WIN32 limitation of 4096 characters on the command line (can't have too many .o's at once for linking or making the library). – whitey04 Aug 17 '11 at 20:22
  • 3
    Be aware that if two of your libraries contain members with the same names, then the naive approach of unpack-libraries-into-directory-and-repack will fail --- on of the members will be lost. You'll need to rename one of the members to avoid a clash. – David Given Jun 05 '13 at 14:46
9

On Unix like systems, the ld and ar utilities can do this. Check out http://en.wikipedia.org/wiki/Ar_(Unix) or lookup the man pages on any linux box or through google, e.g 'unix man ar'.

Please note that you might be better off linking to a shared (dynamic) library. This would add a dependency to your executable but will dramatically reduce its size, especially if you're writing a graphic application.

Adam Bellaire
  • 99,441
  • 19
  • 144
  • 160
Avner
  • 5,582
  • 2
  • 26
  • 32
7

On Linux or MinGW or Cygwin, with GNU toolchain:

ar -M <<EOM
    CREATE libab.a
    ADDLIB liba.a
    ADDLIB libb.a
    SAVE
    END
EOM
ranlib libab.a

Or if you can keep the existence of liba.a and libb.a:

ar crsT libab.a liba.a libb.a

On Windows, with MSVC toolchain:

lib.exe /OUT:libab.lib liba.lib libb.lib
Heefan
  • 357
  • 1
  • 8
Star Brilliant
  • 2,418
  • 19
  • 28
0

I'm not sure how to physically combine them into a single file, however you could employ an abstraction of a sort and just include a single "AllMyLibs.a/h" which in turn includes everything you want. You could also put this into the location where your compiler searches for libraries, so it would work for any project.

P.S. - Out of curiosity, why do you dislike including single libs?

Adam Tyszecki
  • 49
  • 1
  • 5
Ed.
  • 1,378
  • 4
  • 16
  • 26
0

Maybe I'm misunderstanding, but don't you only have to ship the libs if the end-user code calls them directly? If all the access to Jpeg methods etc is from your code in your static library, then just link the libs into your lib.

I.e.

----------------
| End-user exe |
----------------
      |
      | makes calls to
      |
      v
 --------------------
 | Your static lib.a | 
 --------------------
         | makes calls to and links
         v
     ------------------------------------ .....
     |                    |         |
  -------------    -------- ---------- 
  | libjpeg.a |    |libz.a| |libpng.a|
  -------------    -------- ----------

I.e it's only an issue if end code needs to make direct calls into libz.a, libpng.a etc.

If the app code has a legitimate need to call libz.a, for example, then that would as mentioned above be a case for using a dynamic module.

PS: Do I get an artists badge? :)

Greg Whitfield
  • 5,521
  • 2
  • 27
  • 32
  • 15
    No, you do not link when you create "your static lib.a". Linking is only performed when creating the final executable. This is precisely the reason the question was asked. – Trent May 22 '09 at 22:01
  • I'm sorry Trent you are incorrect. Linking applies to libraries, not just EXE's. – Greg Whitfield May 18 '10 at 11:54
  • 3
    @Greg; Trent is right. Creating a library is just putting object files together in an archive with a symbol table. There is no compiler (cc) or linker (ld) just the archiver tool (ar). – whitey04 Aug 19 '11 at 22:15
  • @whitey04: Strictly speaking, you are correct or course. But I was talking in the looser sense of using libs as input to other libs. At least, I was originally although my comment to Trent may contradict that - there was months between them, after all... Maybe I should have said 'makes calls to and references' in my diagram. I hesitate to quote Wikipedia - we know how reliable that can be ;) - but see the image on this page to see what I was driving at - http://en.wikipedia.org/wiki/Linker_(computing) – Greg Whitfield Aug 27 '11 at 17:44
  • I know this is an old discussion, but I've run into the same thing recently, and I have to go with Trent. I built a static library which makes calls into `glib` (also a static library). But nothing from glib got included in my library file, the calls are just left as unresolved. If you try to link to my library without also linking to `glib`, these unresolved calls cause the linker to fail. I could use `ar` to package `glib` and my library together into one, but that's almost certainly a bad idea (eventually will lead to duplicate definitions for someone), and will make my library huge. – brianmearns Oct 29 '13 at 12:37
  • I agree. Trent is right. I know it's years ago this came up, but I believe I was thinking about it in terms of DLL's, rather than static libs. I'm still proud of my artwork though :) – Greg Whitfield Nov 03 '13 at 23:40
  • The issue with doing this is that in your End-user exe you will have to explicitly link to all the static libs below it, so libjpeg, libz, and libpng. This is actually exactly the application I'm running into, and there a lot more image formats in my case so I'll have a very long list of links – ds-bos-msk Dec 29 '13 at 19:18
-2

Combining several third-party libraries into one could create more problems for you--for instance, if two of those libraries define a common symbol which your program doesn't use. Now you've got to extract all (or all-but-one) of the instances of the common symbol before you combine the libraries.

tuxedo
  • 377
  • 1
  • 7