ld-linux.so查找共享库的顺序

本文详细介绍了ld-linux.so在运行时及编译时查找共享库的顺序,包括默认搜索路径、环境变量设置以及配置文件指定等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ld-linux.so查找共享库的顺序

    Glibc安装的库中有一个为ld- linux.so.X,其中X为一个数字,在不同的平台上名字也会不同。可以用ldd查看:
    
     引用
    
    #ldd /bin/cat
    
     linux-gate.so.1 => (0x00bfe000)
    
    libc.so.6 => /lib/libc.so.6 (0x00a4a000)
    
    /lib/ld-linux.so.2 (0x00a28000)
    
    最后一个没有=>的就是。其中第一个不是实际的库文件,你是找不到的,它是一个虚拟库文件用于和kernel交互。
    
    ld-linux.so是专门负责寻找库文件的库。以cat为例,cat首先告诉ld-linux.so它需要libc.so.6这个库文件,ld-linux.so将按一定顺序找到libc.so.6库再给cat调用。
    
    那ld-linux.so又是怎么找到的呢?其实不用找,ld-linux.so的位置是写死在程序中的,gcc在编译程序时就写死在里面了。Gcc写到程序中ld-linux.so的位置是可以改变的,通过修改gcc的spec文件。
    
    运行时,ld-linux.so查找共享库的顺序
    
    (1)ld-linux.so.6在可执行的目标文件中被指定,可用readelf命令查看
    
    (2)ld-linux.so.6缺省在/usr/lib和lib中搜索;当glibc安装到/usr/local下时,它查找/usr/local/lib
    
    (3)LD_LIBRARY_PATH环境变量中所设定的路径
    
    (4)/etc/ld.so.conf(或/usr/local/etc/ld.so.conf)中所指定的路径,由ldconfig生成二进制的ld.so.cache中
    
    编译时,ld-linux.so查找共享库的顺序
    
    (1)ld-linux.so.6由gcc的spec文件中所设定
    
    (2)gcc --print-search-dirs所打印出的路径,主要是libgcc_s.so等库。可以通过GCC_EXEC_PREFIX来设定
    
    (3)LIBRARY_PATH环境变量中所设定的路径,或编译的命令行中指定的-L/usr/local/lib
    
    (4)binutils中的ld所设定的缺省搜索路径顺序,编译binutils时指定。(可以通过“ld --verbose | grep SEARCH”来查看)
    
    (5)二进制程序的搜索路径顺序为PATH环境变量中所设定。一般/usr/local/bin高于/usr/bin
    
    (6)编译时的头文件的搜索路径顺序,与library的查找顺序类似。一般/usr/local/include高于/usr/include
所有依赖.a和.h都已到了main.c的目录下lib和include目录 arm-linux-gnueabihf-sigmastar-9.1.0-gcc main.c -o player -I ./include/ -L ./lib/ /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: /tmp/ccDRtqQa.o: in function `main': main.c:(.text+0x1d2): undefined reference to `stream_player_create' /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x214): undefined reference to `stream_player_set_callback' /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x22a): undefined reference to `stream_player_play_url' /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x250): undefined reference to `stream_player_destroy' /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x278): undefined reference to `stream_player_buffer_level' /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x2b4): undefined reference to `stream_player_stop' /opt/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/9.1.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x2f0): undefined reference to `stream_player_destroy' collect2: error: ld returned 1 exit status
07-08
make[1]: Entering directory '/openwrt/openwrt-15.05.1' make[2]: Entering directory '/openwrt/openwrt-15.05.1/package/helloworld/helloworld' rm -f /openwrt/openwrt-15.05.1/bin/ar71xx/packages/base/helloworld_* rm -f /openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/stamp/._installed rm -f /openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/packages/.list /openwrt/openwrt-15.05.1/staging_dir/host/packages/.list rm -rf /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld-1.0 make[2]: Leaving directory '/openwrt/openwrt-15.05.1/package/helloworld/helloworld' make[1]: Leaving directory '/openwrt/openwrt-15.05.1' make[1]: Entering directory '/openwrt/openwrt-15.05.1' make[2]: Entering directory '/openwrt/openwrt-15.05.1/package/libs/toolchain' mkdir -p /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain touch /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/.prepared_0c0f7031f78967ca687e5628984dfab9 rm -f /openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/stamp/.toolchain_installed (cd /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/./; if [ -x ./configure ]; then find /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/ -name config.guess | xargs -r chmod u+w; find /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/ -name config.guess | xargs -r -n1 cp --remove-destination /openwrt/openwrt-15.05.1/scripts/config.guess; find /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/ -name config.sub | xargs -r chmod u+w; find /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/ -name config.sub | xargs -r -n1 cp --remove-destination /openwrt/openwrt-15.05.1/scripts/config.sub; AR="mips-openwrt-linux-uclibc-gcc-ar" AS="mips-openwrt-linux-uclibc-gcc -c -Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -msoft-float" LD=mips-openwrt-linux-uclibc-ld NM="mips-openwrt-linux-uclibc-gcc-nm" CC="mips-openwrt-linux-uclibc-gcc" GCC="mips-openwrt-linux-uclibc-gcc" CXX="mips-openwrt-linux-uclibc-g++" RANLIB="mips-openwrt-linux-uclibc-gcc-ranlib" STRIP=mips-openwrt-linux-uclibc-strip OBJCOPY=mips-openwrt-linux-uclibc-objcopy OBJDUMP=mips-openwrt-linux-uclibc-objdump SIZE=mips-openwrt-linux-uclibc-size CFLAGS="-Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -msoft-float -mips16 -minterlink-mips16 " CXXFLAGS="-Os -pipe -mno-branch-likely -mips32r2 -mtune=34kc -fno-caller-saves -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -msoft-float -mips16 -minterlink-mips16 " CPPFLAGS="-I/openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/include -I/openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/include -I/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/usr/include -I/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/include " LDFLAGS="-L/openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/usr/lib -L/openwrt/openwrt-15.05.1/staging_dir/target-mips_34kc_uClibc-0.9.33.2/lib -L/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/usr/lib -L/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib " ./configure --target=mips-openwrt-linux --host=mips-openwrt-linux --build=x86_64-linux-gnu --program-prefix="" --program-suffix="" --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/lib --sysconfdir=/etc --datadir=/usr/share --localstatedir=/var --mandir=/usr/man --infodir=/usr/info --disable-nls ; fi; ) rm -f /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/.configured_* touch /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/.configured_yyy cp -fpR /openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libuClibc-*.so /openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libcrypt-*.so /openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libm-*.so /openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libpthread-*.so /openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/ cp: cannot stat '/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libuClibc-*.so': No such file or directory cp: cannot stat '/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libcrypt-*.so': No such file or directory cp: cannot stat '/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libm-*.so': No such file or directory cp: cannot stat '/openwrt/openwrt-15.05.1/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libpthread-*.so': No such file or directory Makefile:636: recipe for target '/openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/.built' failed make[2]: *** [/openwrt/openwrt-15.05.1/build_dir/target-mips_34kc_uClibc-0.9.33.2/toolchain/.built] Error 1 make[2]: Leaving directory '/openwrt/openwrt-15.05.1/package/libs/toolchain' package/Makefile:191: recipe for target 'package/libs/toolchain/compile' failed make[1]: *** [package/libs/toolchain/compile] Error 2 make[1]: Leaving directory '/openwrt/openwrt-15.05.1' /openwrt/openwrt-15.05.1/include/toplevel.mk:181: recipe for target 'package/helloworld/compile' failed make: *** [package/helloworld/compile] Error 2
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值