如何提取包含重复对象文件的静态库?

Gus*_*ira 7 linux static-libraries unix-ar libraries

我正在尝试构建一个合并两个静态库的大型静态库.我正在使用'ar'命令,例如,从'aa'和'ba'中提取对象,然后再次使用'ar'重新组合这些对象:

$ ar x a.a
$ ar x b.a
$ ar r merged.a *.o
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不能用于我的目的,因为aa具有相同名称的不同对象.'ar'命令用于提取重复的对象,并用相同的名称替换已经提取的对象.即使具有相同的名称,这些对象也有不同的符号,因此我得到了未定义的引用,因为某些符号与被替换的文件一起被遗漏.

我无法访问原始对象,并且已经尝试了'ar xP'和'ar xv'以及许多'ar stuff'.有没有人可以帮我展示如何合并这些库?

提前致谢.

Gus*_*ira 2

我尝试了“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 + '.'
Run Code Online (Sandbox Code Playgroud)