How to Pack Multiple Library Archives (.A) into One Archive File

How to pack multiple library archives (.a) into one archive file?

Just tried this on my machine and the problem seems to be that you need to extract the objects from the archives before adding them to the new archive:

ar x libsmall1.a
ar x libsmall2.a
ar rcs libbig.a *.o

Simply running ar rcs like you did produced an archive which contained two .a files, but tools (e.g. nm) were unwilling to look deeper into these files.

How to merge two ar static libraries into one?

You can extract the object from both the .a files and create your .a file using the extracted .os:

ar -x libabc.a
ar -x libxyz.a
ar -c libaz.a *.o

Combining static libraries

1/ Extract ALL of the object files from each library (using ar) and try to compile your code without the libraries or any of the object files. You'll probably get an absolute bucket-load of undefined symbols. If you get no undefined symbols, go to step 5.

2/ Grab the first one and find out which object file satisfies that symbol (using nm).

3/ Write down that object file then compile your code, including the new object file. You'll get a new list of undefined symbols or, if there's none, go to step 5.

4/ Go to step 2.

5/ Combine all the object files in your list (if any) into a single library (again with ar).

Bang! There you have it. Try to link your code without any of the objects but with the new library.

This whole thing could be relatively easily automated with a shell script.

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

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.

Merge multiple .so shared libraries

Merging multiple shared libraries into one is indeed practically impossible on all UNIXen, except AIX: the linker considers the .so a "final" product.

But merging archives into .so should not be a problem:

gcc -shared -o c.so -Wl,--whole-archive a.a b.a -Wl,--no-whole-archive

Compile multiple C source fles into a unique object file

You can't compile multiple source files into a single object file. An object file is the compiled result of a single source file and its headers (also known as a translation unit).

If you want to combine compiled files, it's usually combined into a static library using the ar command:

$ ar cr libfoo.a file1.o file2.o file3.o

You can then use this static library when linking, either passing it directly as an object file:

$ gcc file4.o libfoo.a -o myprogram

Or linking with it as a library with the -l flag

$ gcc file4.o -L. -lfoo -o myprogram


Related Topics



Leave a reply



Submit