我正在尝试构建一个合并两个静态库的大型静态库。我正在使用’ar’命令,例如从’a.a’和’b.a’中提取对象,然后重新组装这些对象……
这里有一个C ++代码,它将许多库合并为一个新的库,而不会覆盖可能的重复对象: http://bazaar.launchpad.net/~paulsepolia/+junk/arbet/files/head:/0025_arbet_FINAL/
我试过'ar p',但与朋友交谈后决定以下python解决方案可能会更好。现在可以提取重复的目标文件。
def extract_archive(pathtoarchive, destfolder) : archive = open(pathtoarchive, 'rb') global_header = archive.read(8) if global_header != '!<arch>\n' : print "Oops!, " + pathtoarchive + " seems not to be an archive file!" exit() if destfolder[-1] != '/' : destfolder = destfolder + '/' print 'Trying to extract object files from ' + pathtoarchive # We don't need the first and second chunk # they're just symbol and name tables content_descriptor = archive.readline() chunk_size = int(content_descriptor[48:57]) archive.read(chunk_size) content_descriptor = archive.readline() chunk_size = int(content_descriptor[48:57]) archive.read(chunk_size) unique_key = 0; while True : content_descriptor = archive.readline() if len(content_descriptor) < 60 : break chunk_size = int(content_descriptor[48:57]) output_obj = open(destfolder + pathtoarchive.split('/')[-1] + '.' + str(unique_key) + '.o', 'wb') output_obj.write(archive.read(chunk_size)) if chunk_size%2 == 1 : archive.read(1) output_obj.close() unique_key = unique_key + 1 archive.close() print 'Object files extracted to ' + destfolder + '.'
您可以重命名对象 - 在链接期间,它们的名称并不意味着什么。这应该工作:
mkdir merge-objs && cd merge-objs && ar x ../a.a && for j in *.o; do mv $j a-$j; done && ar x ../b.a && ar r ../merged.a *.o && cd .. && rm -rf merge-objs