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?
6 Answers
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>

- 30,738
- 21
- 105
- 131

- 26,961
- 9
- 82
- 99
-
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
-
3Be 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
On Linux, MinGW, or Cygwin, with the 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 the Microsoft Visual C++ toolchain:
lib.exe /OUT:libab.lib liba.lib libb.lib

- 30,738
- 21
- 105
- 131

- 2,916
- 24
- 32
-
May you elaborate more about MSCV toolchain in step-by-step please? Thank. – javaLover Mar 29 '17 at 02:06
-
1@javaLover I would like to but sorry I do not own any Windows PC right now. – Star Brilliant Mar 30 '17 at 05:10
-
What do you mean by "keep the existence of `liba.a` and `libb.a`"? Can you elaborate? – Peter Mortensen Jun 27 '22 at 21:36
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 it will dramatically reduce its size, especially if you're writing a graphic application.

- 30,738
- 21
- 105
- 131

- 5,791
- 2
- 29
- 33
-
1
-
1Smaller image size is usually better. Graphics libraries (xlib et. al.) tend to be very large and so make very large executables if linked statically. – Avner Dec 30 '13 at 06:28
-
Ohh so you meant GUI applications. I thought you were referring to graphics as in 3D engines with GPU shaders and stuff... – ds-bos-msk Jan 01 '14 at 10:43
-
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.

- 30,738
- 21
- 105
- 131

- 1,404
- 4
- 17
- 26
Maybe I'm misunderstanding, but don't you only have to ship the libraries 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 libraries into your library.
I.e.,
---------------------
| End-user EXE file |
---------------------
|
| 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 the end code needs to make direct calls into libz.a, libpng.a, etc.
If the application 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.

- 30,738
- 21
- 105
- 131

- 5,649
- 2
- 30
- 32
-
18No, 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
-
4@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
-
1I 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
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.

- 30,738
- 21
- 105
- 131

- 397
- 1
- 7