具有相同名称的目标文件的静态库(AR) [英] Static library having object files with same name (ar)

查看:307
本文介绍了具有相同名称的目标文件的静态库(AR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点上下文.假设我有源文件,这些文件需要以静态库结尾.假设有两个cpp文件a.cppa.cpp位于两个不同的子目录中.像这样:

foo/a.h
foo/a.cpp
bar/a.h
bar/a.cpp

它们的内容没有冲突,完全不同.文件名是相同的.

现在,在编译时,我最终得到了两个a.o文件.

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

如果我现在使用

创建一个静态库

ar rcs libfoobar.a foo/a.o bar/a.o

我可以看到运行nm libfoobar.a的静态库中的两个文件.似乎很好.

问题

我可以看到的问题是,如果我分别为foo/a.obar/a.o运行ar命令并将它们放在同一静态库中.现在,后一个对象文件将覆盖前一个对象,因此在运行nm libfoobar.a时,我仅在库中看到了后一个对象.我猜是由于相同的目标文件名而导致的.

使用ar创建静态库时,我应该总是一次组合所有对象吗?还是多次运行ar也可以一次将所有对象收集到同一静态库中,这还可以吗? ?对于此示例,我可以看到前者的作品,而看不到后者.

当一个a.cpp发生更改并且需要更改静态库时,事情将如何工作? ar会在库中找到正确的a.cpp进行更改吗?

这只是一个小示例,但考虑一个包含许多文件且某些文件具有相同名称的大型项目.如果现在要创建一个库,则可能也会遇到这种情况.

总的来说,这仅仅是组织图书馆的方式,文件的命名方式还是组织不完善的事情?

解决方案

您必须将ar视为非常老的文件存档器.它甚至对归档目录一无所知. (一些档案是扁平的)

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

man ar,选项r:

r将文件成员...插入存档(替换).此操作与q的不同之处在于,如果其名称与要添加的名称相匹配,则会删除任何先前存在的成员.

尝试运行ar t libfoobar.a,您将仅看到a.o文件,因为ar没有在归档文件中存储目录名称.

因此,如果要使用ar更新库中的某些目标文件,则必须对放入ar归档中的所有目标文件进行不同的命名(UPD).

ar rcs lib.a foo/a.o bar/a.o替换了lib.a中找到的a.o,但不检查添加的文件是否存在名称冲突.

其他情况:ar rcs lib.a foo/a.oar rcs lib.a bar/a.o将第一个a.o存储在存档中,然后第二个ar将在存档中找到较旧的a.o并替换旧文件.

A bit context. Let's say I have source files, which need to end up in a static library. Let's say there are two cpp files a.cpp and a.cpp located in two different subdirectories. Something like this:

foo/a.h
foo/a.cpp
bar/a.h
bar/a.cpp

Their content is not clashing and is completely different. The file names are just same.

Now, when compiling I end up with two a.o files of course.

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

If I create a static lib now with

ar rcs libfoobar.a foo/a.o bar/a.o

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

Problem

The problem I can see is if I run the ar command individually for foo/a.o and bar/a.o putting them in the same static library. Now the latter object file will overwrite the former, so when running nm libfoobar.a I just see the latter object in the library. I'm guessing this is the case due to the same object file name.

When creating a static library with ar, should I always combine all objects in one go or is it also fine to run ar multiple times collecting a part of objects at a time all ending up in the same static library? For this example I can see the former works, but not the latter.

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

This is just a small example, but consider a large project with many files and some have the same name. If you now want to create a single library you could end up with this situation as well.

In general, is this just poor organization of how libraries are composed, how files are named or is there something else to this to make things work?

解决方案

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

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

man ar, option r:

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

Try to run a ar t libfoobar.a and you will see only a.o files because ar didnt store directory name in the archive.

So you must name all object files putted in ar archive differently (UPD) if you want to do an update of some object files in library with ar

The ar rcs lib.a foo/a.o bar/a.o does a replace of a.o found in the lib.a, but it does not check added files for name collision.

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

这篇关于具有相同名称的目标文件的静态库(AR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆