GhassanPL
GhassanPL

Reputation: 2734

How can I combine several C/C++ libraries into one?

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?

Upvotes: 48

Views: 41470

Answers (6)

Star Brilliant
Star Brilliant

Reputation: 3136

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

Upvotes: 10

Greg Whitfield
Greg Whitfield

Reputation: 5739

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.

Upvotes: 0

tuxedo
tuxedo

Reputation: 407

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.

Upvotes: -1

Judge Maygarden
Judge Maygarden

Reputation: 27593

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>

Upvotes: 28

Avner
Avner

Reputation: 5831

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.

Upvotes: 9

Ed.
Ed.

Reputation: 1404

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.

Upvotes: 0

Related Questions