[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating a static lib from objects and another static lib

From: Martin Willers
Subject: Re: Creating a static lib from objects and another static lib
Date: Tue, 18 Mar 2008 22:59:09 +0100
User-agent: KMail/1.9.1

> > Folks I am trying to embed a static lib in a new lib that I am
> > creating with more objects
> >
> > ar rcs libnew.a a.o b.o oldlib.a
> >
> > This succeeds but when I link against this new library the symbols
> > from the old lib are missing
> >
> > Any ideas ?
> Note this is not really a problem with GNU make.  You'll probably have
> better support on a list more targeted at compilers or binutils.
> However, on most systems ar cannot take an already-existing archive as
> one of the "objects", and extract all its objects and install them into
> a new archive.  That's simply a supported functionality: you can't do
> it.

With GNU ar you can, using an ar-script. On the command-line, e.g.
  $ ar -M
  AR >create libnew.a
  AR >addmod a.o
  AR >addmod b.o
  AR >addlib oldlib.a
  AR >save
  AR >end
This will create libnew.a with the contents of oldlib.a merged with a.o and 

Of course, you can pipe a make-generated list of these commands to ar -M
and do this as part of your build process. One of my Makefiles contains 
something like this:

# $1 = target name, $2 = .o files to include, $3 = .a files to include
makearchive = "create $(1)\n  $(foreach mod,$(2),addmod $(mod)\n)  $(foreach 
lib,$(3),addlib $(lib)\n)  save\nend\n"

prerequisites = a.o b.o libnew.a

libstatic.a: $(prerequisites)
        echo -e $(call makearchive,$@,$(filter %.o,$^),$(filter %.a,$^)) | 
$(AR) -M

Disadvantages of ar-scripts are:
1. Certain characters are not allowed in filenames given to "addmod" 
or "addlib", for example '+'.
2. According to the GNU ar manual, this feature's "only purpose [...] is to 
ease the transition to gnu ar for developers who already have scripts written 
for the MRI 'librarian' program" (I have no idea what that "librarian" 
program is, though). This probably does not make it the most supported 
feature of GNU ar.

> In your case the solution is actually pretty simple: just copy the old
> library to the new library, then use ar to add in the new objects:
>       cp oldlib.a libnew.a && ar rs libnew.a a.o b.o

This is certainly a better approach, for the reasons cited above.
(It wasn't suitable for me, because one of our application is build as a 
collection of static libraries, and I needed to break down some of those 
libraries into smaller libraries again, resulting in the need to build a 
static library from a couple of other static libraries.)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]