3

I am working on various flavors of Linux and I need to combine multiple static libraries such as

foo1.a
foo bar.a
foo2.a

into one single combo static library (note that the second file has a space in it's name).

I've seen stackoverflow articles that explain how to do this with an ar mri script. The suggestion is to create a file named combine.ar with contents such as

CREATE comboLib.a
ADDLIB foo1.a
ADDLIB foo bar.a
ADDLIB foo2.a
VERBOSE
SAVE
END

And then use the command ar -M < combine.ar.

However, the ar script language treats spaces as a way to add two items, so it sees the second line as add library foo and library bar.a

I've tried the following with no luck

ADDLIB "foo bar.a"
ADDLIB foo\ bar.a
ADDLIB 'foo bar.a'

How can this be done?

John Rocha
  • 1,656
  • 3
  • 19
  • 30
  • 3
    A simple workaround might be to rename or symlink the library `foo bar.a` to a name without a space. – Bodo Jul 03 '19 at 16:39
  • Now you know one more reason not to use file names (or path names) with spaces in them when programming. It wreaks havoc, sooner rather than later. Stick to the [portable filename character set](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282) defined by POSIX — which consists of the (unaccented Latin) alphanumeric characters, underscore, dash, dot. Anything else is risky — sooner or later, you'll run into trouble. – Jonathan Leffler Jul 03 '19 at 21:19

1 Answers1

3

I don't know if it is possible to use file names with a space in a MRI script. You can rename or symlink the library file foo bar.a to a name without space as a simple workaround.

If you don't insist on using a MRI script you can do this with a series of ar commands.

The following script assumes that all library files are in the same directory and the temporary directory does not conflict with something else.

for lib in foo1.a 'foo bar.a' foo2.a
do
  rm -rf tmpdir   # make sure we do not have any existing file
  mkdir tmpdir    # place to extract members
  cd tmpdir
  # x = extract all memebers, o = keep original file date
  ar xo ../"$lib"
  # r = add/replace member, c = create archive without warning
  ar rc ../comboLib.a *
done
rm -rf tmpdir     # clean up

Edit: As modern systems don't need ranlib, I removed the s modifier from the ar r command.

See https://linux.die.net/man/1/ar

Bodo
  • 9,287
  • 1
  • 13
  • 29