How to merge two “ar” static libraries into one?

前端 未结 6 831
悲哀的现实
悲哀的现实 2020-11-22 14:56

I have 2 static Linux libraries, created by ar cr, libabc.a and libxyz.a.
I want to merge them into one static library libaz

6条回答
  •  一向
    一向 (楼主)
    2020-11-22 15:46

    If you simply do it as :

    ar x a.a
    ar x b.a
    ar c c.a  *.o 
    

    you will lost some object files if there are members with same name in both a.a and b.a so, you need to extract members of different archives into different folder:

    ar x a.a && mv *.o a_objs
    ar x b.a && mv *.o b_objs
    ar c c.a a_objs/*.o b_objs/*.o
    

    further more, it is posible that there are multiple members of same name in one archive (say in a.a), if you run ar x a.a, you will get only one for those members of same name.

    The only way to extract all members of same name in one archive is to specify the member number by option 'N':

    ar xN 1 a.a  xxx.c.o && mv xxx.c.o xxx.c.1.o
    ar xN 2 b.a  xxx.c.o && mv xxx.c.o xxx.c.2.o
    ...
    

    this would be a tedious work, so you will have to write a more sophisticate script to do that job.

    One optional solutions is that you can combine multiple archives into one shared library:

    g++ -shared -o c.so -Wl,--whole-archive a.a b.a 
    

    this way the linker will handle all things for you!

提交回复
热议问题