Static library with object files with the same name (ar)

A bit of context. Say I have source files that should be in a static library. Let's say there are two cpp a.cpp and a.cpp located in two different subdirectories. Something like that:

 foo/ah foo/a.cpp bar/ah bar/a.cpp 

Their content does not collide and is completely different. The file names are the same.

Now, when compiling, I get, of course, two ao files.

 gcc -c foo/a.cpp -o foo/ao gcc -c bar/a.cpp -o bar/ao 

If I create a static lib now with

 ar rcs libfoobar.a foo/ao bar/ao 

I can see both files inside the nm libfoobar.a static library nm libfoobar.a . Seems beautiful.

Problem

The problem that I see is that I run the ar command separately for foo/ao and bar/ao , putting them in the same static library. Now the last file of the object will overwrite the first, so when I start nm libfoobar.a I just see the last object in the library. I guess this is because of the same object name.

When creating a static library with ar , should I always merge all objects at a time or is it also normal to run ar several times collecting some of the objects at a time, all ending in the same static library? In this example, I see the first works, but not the last.

How will everything work when one a.cpp changes and the static library needs to change? Will ar find the correct a.cpp to change in the library?

This is just a small example, but consider a large project with many files, and some with the same name. If now you want to create one library, you can cope with this situation too.

In general, is this just a poor organization of how libraries are compiled, what are the files called, or is there something else for this?

+7
source share
3 answers

You should think of ar as a very old file archiver. He doesn't even know anything about directory archiving. (ar archives are flat)

(man ar): ar - create, modify, and extract from archives

man ar, option r :

r Insert the file member ... into the archive (with replacement). This operation differs from q in that any previously existing members are deleted if their names match the ones added.

Try running ar t libfoobar.a and you will only see ao files because ar does not store the directory name in the archive.

So, you should name all the object files placed in the ar archive in different ways (UPD) if you want to update some object files in the library with ar

ar rcs lib.a foo/ao bar/ao does the ar rcs lib.a foo/ao bar/ao replacement found in lib.a but does not check the added files for name collisions.

Another case: ar rcs lib.a foo/ao and ar rcs lib.a bar/ao will store the first ao file in the archive, then the second ar will find the older ao in the archive and replace the old file.

+3
source

A library is just a collection of functions and / or data that have been grouped by object files in a library, and these object files have a name. These names do not play any role except to update / retrieve / delete.

Therefore, it is perfectly legal to have two identical names for two or more object files. When updating the library, the librarian replaces the first object with the name you replace, and no longer looks.

This is not a reasonable thing.

+3
source

I can solve only part of your question. From the Makefile syntax, we see that it was the usual way - to update only one object. But, for example, automake's approach is to rebuild the library from scratch, even if one file is modified. Now this is not a big problem ...

Sorry, you don’t have unix now, so we’ll still wait for an expert answer :)

From my own experience, I would not recommend having two files with the same name in a static library.

+1
source

All Articles