【发布时间】:2021-12-14 12:38:02
【问题描述】:
我正在尝试编译以下组件:
appl - 链接到动态共享库 (libwrapper.so) 的应用程序
libwrapper.so - appl 使用的库。该库从 libbackend 存档中调用函数。
libbackend.a - 使用 libc 函数的静态存档。
我想确保 libbackend 调用的 libc 函数始终使用固定的 libc 实现。所以我也在我的编译环境中将所需的 libc.a 归档到 libbackend 中。
我想实现的是,当libbackend调用任何libc函数时,应该调用编译环境中归档的libc函数,而不是目标环境中libc.so版本的函数。
这可以实现吗?此外,是否可以在不改变 appl 的编译方式的情况下实现这一点?即,可以通过仅更改 libwrapper 和 libbackend 的 makefile 来实现吗?
这是我迄今为止尝试过的,但不起作用:
[dev-env]# ls
appl.c backend.c backend.h Makefile wrapper.c wrapper.h
[dev-env]# cat appl.c
#include <stdio.h>
#include "wrapper.h"
void main()
{
printf("in appl\n");
wrapper();
}
[dev-env]# cat wrapper.h
void wrapper();
[dev-env]# cat wrapper.c
#include <stdio.h>
#include "wrapper.h"
#include "backend.h"
void wrapper()
{
printf("In wrapper\n");
backend();
}
[dev-env]# cat backend.h
void backend();
[dev-env]# cat backend.c
#include <stdio.h>
#include <gnu/libc-version.h>
#include "backend.h"
void backend()
{
printf("in backend\n");
printf("GNU libc version: %s\n", gnu_get_libc_version());
}
[dev-env]# cat Makefile
LIBC=$(shell gcc --print-file-name=libc.a)
all: libbackend.a libwrapper.so appl
libbackend.a: backend.c backend.h
gcc -static -fPIC -c backend.c -o backend.o
ar rcs libbackend.a $(LIBC) backend.o
libwrapper.so: wrapper.c wrapper.h libbackend.a
gcc -c wrapper.c
gcc -shared -o libwrapper.so wrapper.o libbackend.a
appl: appl.c
gcc -o appl appl.c -L . -lwrapper
clean:
rm *.o *.a *.so appl
[dev-env]#
从编译环境:
[dev-env]# make
gcc -static -fPIC -c backend.c -o backend.o
ar rcs libbackend.a /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/libc.a backend.o
gcc -c wrapper.c
gcc -shared -o libwrapper.so wrapper.o libbackend.a
gcc -o appl appl.c -L . -lwrapper
[dev-env]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
[dev-env]# ./appl
in appl
In wrapper
in backend
GNU libc version: 2.28
在目标环境中:
[target]# ./appl
in appl
In wrapper
in backend
GNU libc version: 2.30
如果我打算工作,在目标上我应该看到版本输出为“2.28”。
【问题讨论】:
-
在这种情况下,“已归档” 是什么意思?例如,“归档的 libc 函数”。如何归档函数?你能详细说明一下吗?
标签: static-libraries glibc libc