需要重建以与提供相同接口的不同库一起使用吗?
我有一个项目“A”,它依赖于“B”。 “B”在启动时动态加载。
想象一下,有一个库“C”满足与“B”相同的接口。使用 B.so 构建的 A.so 可以与 C.so 一起使用而无需重建吗?链接器的工作是找到正确的函数地址吗?
仅通过配置优先考虑 C.so 的 LD_LIBRARY_PATH 就可以“安全”地实现这一点吗?
I have a project "A" that has dependency to "B". "B" is dynamically loaded in start up time.
Imagine there's a library "C" that satisfies the same interface as "B". Can A.so that is built using B.so be used with C.so without rebuild? Is it linker's job to find the correct function's addresses?
Can this be "safely" achieved just by configuring LD_LIBRARY_PATH that prioritizes C.so ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
我似乎误解了这个问题。如果要保持
libb.so
&libc.so
,但使用libc.so
作为某些二进制文件,然后将它们保留在单独的文件夹中,然后使ld_librarypath首先找到libc.so
文件夹。您将在其中创建一个软链接libc.so-> libb.so
。随着环境变量的变化,您可能已经运行需要libc.so
的二进制文件。测试它。让我们定义一个函数
str_mod()
在两个不同的库中具有相同接口。一个( a )将输入字符串中的所有字符都更改为上案例,而另一个( b )将其逆转。我们将拥有一个包装器函数库( c )
str_mod_c()
替换字符串本身。后来调用str_mod()
。一个调用包装器
str_mod_c()
的测试程序。当我们替换Corelib_if.h
lib_aaa.c
:只需将所有字符转换为上caselib_bbb.c
:转换字符串lib_ccc.c
lib_ccc.c < /code>:用默认字符串
test_so.c
替换字符串库 c 替换字符串&amp; a 大写。
同样, c 替换字符串,库 b 将其逆转。
libbbb.so
&amp;运行test_ccc_aaa
创建一个软链接
libaaa.so-&gt; libbbb.so
test_ccc_aaa
仍然链接到libaaa.so
,但执行libbbb.so
OS:Ubuntu 20.04 LTS X64
GCC:9.4
Edit:
I seem to have misunderstood the question. If you want to keep both
libB.so
&libC.so
but uselibC.so
for some binaries, then keep them in separate folders and make LD_LIBRARYPATH to findlibC.so
folder first. Where you'll create a soft-linklibC.so -> libB.so
. You may have run the binary that needslibC.so
separately than others, as the environment variables change. Test it.Let's define a function say
str_mod()
with same interface in two different library. One(A) changes all characters in input string to upper-case, while other(B) reverses it.We'll have a wrapper function library(C)
str_mod_C()
which replaces the string itself. Later callsstr_mod()
.A test program which calls the wrapper
str_mod_C()
. What happens when we replace the corelib_if.h
lib_AAA.c
: Just converts all chars to upper-caselib_BBB.c
: reverses the stringlib_CCC.c
: replaces the string with a default stringtest_so.c
: calls a function from `lib_CCC.cLibrary C replaces the string & A capitalises it.
Again, C replaces the string, library B reverses it.
libAAA.so
with a copy oflibBBB.so
& runtest_CCC_AAA
Created a soft-link
libAAA.so -> libBBB.so
test_CCC_AAA
is still linked tolibAAA.so
but executeslibBBB.so
's codeTested on:
OS : Ubuntu 20.04 LTS x64
GCC : 9.4