创建一个共享对象 C,它依赖于静态库 B,而静态库 B 本身依赖于静态库 A



>Goal

我想创建一个静态链接到libmul.a共享库libsquare.so,而它本身静态链接到libadd.a

情况

我通过将libadd.a链接到它来创建libmul.a。 然后我通过在刚刚创建的libmul.a中链接来创建libsquare.so

libmul.a具有add函数的代码。

问题

当我将其链接到共享对象并查看符号表时,add符号仍然存在,但未定义(*UND*),而不是在.text部分中。

我在Github上有一个MWE演示它。

这里是objdump -t libsquare.so的一部分

libsquare.so:     file format elf64-x86-64
SYMBOL TABLE:
0000000000000000         *UND*  0000000000000000              add
0000000000001119 g     F .text  000000000000001c              square
0000000000001135 g     F .text  000000000000003b              mul
... // some symbols omitted here... See repo for entire table

这里整个objdump -t libmul.a

mul.o:     file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 mul.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 g     F .text  000000000000003b mul
0000000000000000         *UND*  0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000000000         *UND*  0000000000000000 add

In nested archive libadd.a:
add.o:     file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 add.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 g     F .text  0000000000000014 add

本质上,运行的命令是:

cc -fPIC -rdynamic -o add.o -c add.c
cc -fPIC -rdynamic -o mul.o -c mul.c
cc -fPIC -rdynamic -o square.o -c square.c
ar rcs libadd.a add.o
ranlib libadd.a
ar rcs libmul.a mul.o libadd.a
ranlib libmul.a
gcc -shared -Wl,-soname=square -o libsquare.so square.o libmul.a 
cc -fPIC -rdynamic -o test-w-lib.o -c test-w-lib.c
cc -o test-w-lib libsquare.so test-w-lib.o -ldl 
/usr/bin/ld: libsquare.so: undefined reference to `add'
collect2: error: ld returned 1 exit status

没关系,我找到了解决方案。

开个玩笑:)

问题是我的ar命令。通过ar rcs libmul.a mul.o libadd.a创建libmul.a时,它会创建一个名为libmul.a的存档,其中包含mul.olibadd.a,如下所示:

libmul.a
├── mul.o
└── libadd.a
   └── add.o

另请参阅对象转储:

$ objdump libmul.a -t
In archive libmul.a:
mul.o:     file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 mul.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 g     F .text  000000000000003b mul
0000000000000000         *UND*  0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000000000         *UND*  0000000000000000 add

In nested archive libadd.a:
add.o:     file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 add.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 g     F .text  0000000000000014 add

当最终链接libmul.agcc use-lib-mul.c libmul.a时,这也失败了。(因此,整体问题与共享对象无关 - 部分)。

溶液

如注释和此处所述,必须从依赖项 liblibadd.a中解压缩对象以创建另一个 liblibmul.a

所以要正确生成libmul.a

libmul.a: mul.o libadd.a
rm -rf libadd; mkdir libadd; cd libadd; ar x ../libadd.a
ar rcs ${@} ${<} ./libadd/*.o
ranlib ${@}

这将创建:

libmul.a
├── mul.o
└── add.o

最后,要正确生成libsquare.so

libsquare.so: square.o libmul.a
rm -rf libmul;  mkdir libmul; cd libmul; ar x ../libmul.a
$(CC) -shared -Wl,-soname=square -o ${@} ${<} libmul/*.o

然后在.text部分中具有所有必需的符号,并且符号的所有长度均为>00x140x1c0x3b

$ objdump libsquare.so -t
libsquare.so:     file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000              square.c
0000000000000000 l    df *ABS*  0000000000000000              add.c
0000000000000000 l    df *ABS*  0000000000000000              mul.c
0000000000001135 g     F .text  0000000000000014              add
0000000000001119 g     F .text  000000000000001c              square
0000000000001149 g     F .text  000000000000003b              mul
...

最新更新