c - 如何找出哪些文件与 "-lc"链接?

标签 c linux clang cross-compiling libc

花了近一个小时过滤输出

clang -v hello_world.c

我得到以下链接命令:

ld -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
/usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o \
hello_world.o /usr/lib/x86_64-linux-gnu/libc.so \
/usr/lib/x86_64-linux-gnu/crtn.o

是否有更简单的方法来找出 -lc 将扩展为 /usr/lib/x86_64-linux-gnu/libc.so

我需要知道使用了哪些文件,以便将它们复制到另一个系统进行交叉编译。

编辑

看来我应该使用交叉编译工具链。来自 clang 文档:

https://clang.llvm.org/docs/CrossCompilation.html

When you have extracted your cross-compiler from a zip file into a directory, you have to use --sysroot=. The path is the root directory where you have unpacked your file, and Clang will look for the directories bin, lib, include in there.

我在哪里可以获得他们引用的 zip 文件?我对 x86_64-linux-gnu 目标感兴趣。

最佳答案

这将列出链接到 libc.so.6 的所有文件:

for i in `find . -type f -executable`
do
    j=$(ldd $i | grep libc.so.6 | wc -l)
    if [ $j -gt 0 ]
    then
        echo $i
    fi
done

如果您想了解 libc.so.6 的路径是什么,请在原始问题中说明类似于:

ldd `which ld` | sed 's/^[[:space:]]libc.so.6[[:space:]]=>[[:space:]]\(.*\)[[:space:]](.*)/\1/p' | grep --color=never libc.so

将输入路径,显然您可以将 ldd 后面的表达式替换为任何文件名。

从评论来看,有一种直接使用 clang 的方法,但它会产生大量噪音,与 ldd 方式相比,噪音明显难以排除。

clang -Wl,--verbose hello_world.c

将告诉链接器要详细,它最终会告诉您为每个库尝试的所有库路径。

关于c - 如何找出哪些文件与 "-lc"链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56478572/

相关文章:

c - 使用 clock_gettime() 得到不一致的结果

linux - 改变鞭尾鱼的颜色

c++ - Clang 链接 .so 文件

c++ - 在 Qt Creator 中使用 libc++ 而不是 libstdc++

c - 如何在字符串中添加数字? (C)

C 未知类型名称堆栈

c - 求 sum(1)+sum(2)+....+(n) 的总和

PHP 警告 : PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-zts-20131226/pdo_mysql.so'

linux - busybox 初始化脚本。未找到挂载点

c++ - 模板默认参数 SFINAE 与 clang 不明确,适用于 g++