views:

1043

answers:

2

I'm trying to create a single static library which contains object files and existing static libraries which have all been compiled earlier in the build process. Is there an easy way to do this using "ar", or will I need to unpack (ar x library_name) each library and add its object files to my unified library? Thanks!

A: 

You will need to extract the object files from the other libraries and bundle the extracted object files into your composite library.

Also consider incremental loading: ld -r

Jonathan Leffler
+2  A: 

If you want to create a shared library you can use this:

You can use the --whole-archive flag from ld:

--whole-archive
For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

Two notes when using this option from gcc: First, gcc doesn't know about this option, so you have to use -Wl,-whole-archive. Second, don't forget to use -Wl,-no-whole-archive after your list of archives, because gcc will add its own list of archives to your link and you may not want this flag to affect those as well.

For static libraries you may have to extract the objects first.

I found the following for ar

gnu ar can optionally create a thin archive, which contains a symbol index and references to the original copies of the member files of the archives. Such an archive is useful for building libraries for use within a local build, where the relocatable objects are expected to remain available, and copying the contents of each object would only waste time and space. Thin archives are also flattened, so that adding one or more archives to a thin archive will add the elements of the nested archive individually. The paths to the elements of the archive are stored relative to the archive itself.

Maybe you can use such a thin archive.

lothar
The thin archive option is a (relatively) recent one. It is not available on Fedora 8 for instance (ar 2.17.50).
neuro
+1 by the way for an interesting option (--whole-archive) that just saved my life !
neuro