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?

2
  • 3
    A simple workaround might be to rename or symlink the library foo bar.a to a name without a space. Commented Jul 3, 2019 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 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. Commented Jul 3, 2019 at 21:19

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

You should probably run ranlib to create (cleanup?) the indexes after the archive is created.
You shouldn't need to use ranlib, @jww. Modern versions of ar automatically do the relevant indexing job.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.