【问题标题】:Why a static library can depend on a shared a library?为什么静态库可以依赖共享库?
【发布时间】:2021-06-04 06:35:05
【问题描述】:

据我所知,静态库不能依赖于 Linux 中的共享库。但是,当我编译一个链接到 glog.a 和 gflags.a 的程序时,编译器会报告以下错误:

> /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libglog.a(libglog_la-utilities.o):
> In function `google::GetStackTrace(void**, int, int) [clone .part.7]':
> (.text+0xad): undefined reference to `_Ux86_64_getcontext'
> (.text+0xb8): undefined reference to `_ULx86_64_init_local'
> (.text+0xe7): undefined reference to `_ULx86_64_step' (.text+0x100):
> undefined reference to `_ULx86_64_get_reg' (.text+0x120): undefined
> reference to `_ULx86_64_step' collect2: error: ld returned 1 exit
> status

这个编译错误也可以通过链接 unwind.so 来解决。但我觉得奇怪的是,为什么 libglog.a 会依赖共享库?这不是不可能吗?

【问题讨论】:

    标签: linux gcc shared-libraries static-libraries


    【解决方案1】:

    此声明

    在 Linux 中静态库不能依赖共享库

    完全错误。

    静态库只是目标文件的集合。

    如果您可以使用从共享库导入并编译为 .o 文件的代码,您可以将这些 .o 文件收集到库中,现在您拥有一个使用来自共享库的导入的库。

    与直接链接所有目标代码相比,静态库的唯一真正区别在于,该库包含一个符号索引,链接器使用该索引来决定需要链接库中的哪些目标文件。而直接传递给链接器的目标文件总是被链接进来。这对全局初始化行为有重要的影响,而其他的影响很小。

    【讨论】:

    • 非常感谢您的澄清!
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2010-11-17
    相关资源
    最近更新 更多