具有相同名称的目标文件的静态库(AR) [英] Static library having object files with same name (ar)
问题描述
有点上下文.假设我有源文件,这些文件需要以静态库结尾.假设有两个cpp文件a.cpp
和a.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.o
和bar/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).
其他情况: 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 Their content is not clashing and is completely different. The file names are just same. Now, when compiling I end up with two If I create a static lib now with I can see both files inside the static library running Problem The problem I can see is if I run the When creating a static library with How will things work when one 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 (man ar): man ar, option 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 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 Other case: 这篇关于具有相同名称的目标文件的静态库(AR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!ar rcs lib.a foo/a.o bar/a.o
替换了lib.a中找到的a.o,但不检查添加的文件是否存在名称冲突.ar rcs lib.a foo/a.o
和ar rcs lib.a bar/a.o
将第一个a.o存储在存档中,然后第二个ar
将在存档中找到较旧的a.o
并替换旧文件.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
a.o
files of course.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
. Seems fine.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.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.a.cpp
changes and the static library needs to change? Will ar
find the right a.cpp
to change in the library?ar
as very old file archiver. It even doesn't know anything about archiving a directory. (ar archives are flat)ar - create, modify, and extract from archives
r
:
ar t libfoobar.a
and you will see only a.o
files because ar
didnt store directory name in the archive.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.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.