欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

-L -Wl,-rpath-link -Wl,-rpath区别精讲

程序员文章站 2024-03-20 22:23:34
...

目录

 

前言

关于gcc这三个参数,参考了诸多文档后,仍然理解上有偏差,仿照下面博客中的方法,自己调试了一波,总算是理解了。还是建议大家动手实践一下。

参考资料如下:

源码准备

新建三个文件:test.c hello.c world.c ,其源码依赖关系为:test.c 依赖 hello.c;hello.c 依赖 world.c

源码内容

test.c

#include <stdio.h>
void world(void);
void hello(void)
{
    printf("hello ");
    world();
}

hello.c

#include<stdio.h>
void hello(void);
void main(void)
{
    hello();
}

world.c

#include<stdio.h>
void world(void)
{
	printf("world.\n");
}

尝试编译,保证源码没有问题

# -o 指定输出文件
[[email protected] testc]# ls
hello.c  test.c  world.c
[[email protected] testc]# gcc -o hehe *.c
[[email protected] testc]# ls
hehe  hello.c  test.c  world.c
[[email protected] testc]# ./hehe 
hello world.

编译

首先编译world.c

# -shared 编译链接库
# -fPIC 我的理解是编译链接库时给代码动态分配内存
[[email protected] testc]# gcc -shared -fPIC -o libworld.so world.c
[[email protected] testc]# ls
hello.c  libworld.so  test.c  world.c
[[email protected] testc]# ldd libworld.so 
	linux-vdso.so.1 =>  (0x00007ffd7498f000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fcb49815000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fcb49dea000)

#上述命令和下面的等价,建议选取单条命令即可:gcc -shared -fPIC -o libworld.so world.c
# -c 只**预处理,编译,和汇编,也就是他只把程序做成obj文件
[[email protected] testc]# gcc -c -fPIC world.c 
[[email protected] testc]# ls
hello.c  test.c  world.c  world.o
[[email protected] testc]# gcc -shared -fPIC -o libworld.so world.o
[[email protected] testc]# ls
hello.c  libworld.so  test.c  world.c  world.o
[[email protected] testc]# ldd libworld.so 
	linux-vdso.so.1 =>  (0x00007ffd0dfa9000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f4357dcb000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f43583a0000)

编译并链接hello.c

# -lxxx 指定需要动态链接的库文件
# -L 指定动态连接库文件的位置(编译时)
# . 指当前路径
# 下面编译出的libhello.so文件已经显式依赖libworld.so文件,但是没有找到libworld.so的位置
[[email protected] testc]# gcc -shared -fPIC -o libhello.so hello.c -lworld -L.
[[email protected] testc]# ls
hello.c  libhello.so  libworld.so  test.c  world.c
[[email protected] testc]# ldd libhello.so 
	linux-vdso.so.1 =>  (0x00007ffe61b89000)
	libworld.so => not found
	libc.so.6 => /lib64/libc.so.6 (0x00007ff73cc1e000)
	/lib64/ld-linux-x86-64.so.2 (0x00007ff73d1f4000)

调试编译test.c

[[email protected] testc]# ls
hello.c  libhello.so  libworld.so  test.c  world.c

# 编译出错,提示找不到hello
[[email protected] testc]# gcc -o haha test.c 
/tmp/ccQCWcSW.o: In function 'main':
test.c:(.text+0x5): undefined reference to 'hello'
collect2: error: ld returned 1 exit status

# 添加libhello.so的链接索引,并指定库的搜索路径为'.'(当前路径)
# 依然编译失败,提示找不到libworld.so,该库被libhello.so依赖,并提示建议使用-rpath 或 -rpath-link解决
[[email protected] testc]# gcc -o haha test.c -lhello -L.
/usr/bin/ld: warning: libworld.so, needed by ./libhello.so, not found (try using -rpath or -rpath-link)
./libhello.so: undefined reference to 'world'
collect2: error: ld returned 1 exit status

# 手动添加libworld.so的依赖,编译通过,查看haha的链接库,已经显式指出依赖,但是没有找到其位置
[[email protected] testc]# gcc -o haha test.c -lhello -L. -lworld
[[email protected] testc]# ldd haha 
	linux-vdso.so.1 =>  (0x00007fff556ea000)
	libhello.so => not found
	libworld.so => not found
	libc.so.6 => /lib64/libc.so.6 (0x00007f1ff0c97000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f1ff106b000)

# 执行编译出的haha,执行报错,提示找不到依赖库
[[email protected] testc]# ./haha 
./haha: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory

#修改系统环境变量'LD_LIBRARY_PATH',增加索引库的位置,查看依赖OK,执行haha, 结果OK, 清空'LD_LIBRARY_PATH'
[[email protected] testc]# export LD_LIBRARY_PATH=/home/testc/  && echo $LD_LIBRARY_PATH
/home/testc/
[[email protected] testc]# ldd haha 
	linux-vdso.so.1 =>  (0x00007ffd647d2000)
	libhello.so => /home/testc/libhello.so (0x00007fb7aa063000)
	libworld.so => /home/testc/libworld.so (0x00007fb7a9e60000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fb7a9a8e000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fb7aa266000)
[[email protected] testc]# ./haha 
hello world.
[[email protected] testc]# export LD_LIBRARY_PATH=  && echo $LD_LIBRARY_PATH

# 将-lworld 替换为 -Wl,-rpath-link=. ,编译OK,依然找不到索引库,添加LD_LIBRARY_PATH后,执行OK 
[[email protected] testc]# gcc -o haha test.c -lhello -L. -Wl,-rpath-link=.
[[email protected] testc]# ldd haha 
	linux-vdso.so.1 =>  (0x00007ffdf67c0000)
	libhello.so => not found
	libc.so.6 => /lib64/libc.so.6 (0x00007fbdbb94b000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fbdbbd1f000)
[[email protected] testc]# ./haha 
./haha: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
[[email protected] testc]# export LD_LIBRARY_PATH=/home/testc  && echo $LD_LIBRARY_PATH
/home/testc
[[email protected] testc]# ldd haha 
	linux-vdso.so.1 =>  (0x00007fff89504000)
	libhello.so => /home/testc/libhello.so (0x00007fc9e75c6000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fc9e71f3000)
	libworld.so => /home/testc/libworld.so (0x00007fc9e6ff1000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fc9e77c9000)
[[email protected] testc]# ./haha 
hello world.
[[email protected] testc]# export LD_LIBRARY_PATH=  && echo $LD_LIBRARY_PATH

# 将-Wl,-rpath-link=. 换成 -Wl,-rpath=. 编译OK, 查看链接库OK,执行OK 
# 修改LD_LIBRARY_PATH后,链接库的位置没有变化
[[email protected] testc]# gcc -o haha test.c -lhello -L. -Wl,-rpath=.
[[email protected] testc]# ls
haha  hello.c  libhello.so  libworld.so  test.c  world.c
[[email protected] testc]# ldd haha 
	linux-vdso.so.1 =>  (0x00007fff86195000)
	libhello.so => ./libhello.so (0x00007f4c11254000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f4c10e81000)
	libworld.so => ./libworld.so (0x00007f4c10c7f000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f4c11457000)
[[email protected] testc]# ./haha 
hello world.
[[email protected] testc]# export LD_LIBRARY_PATH=/home/testc/  && echo $LD_LIBRARY_PATH
/home/testc/
[[email protected] testc]# ldd haha 
	linux-vdso.so.1 =>  (0x00007ffc9f36c000)
	libhello.so => ./libhello.so (0x00007f35cf07c000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f35ceca9000)
	libworld.so => ./libworld.so (0x00007f35ceaa7000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f35cf27f000)
[[email protected] testc]# export LD_LIBRARY_PATH=  && echo $LD_LIBRARY_PATH


结论

  • 编译时链接库需要分为两类: 直接引用 间接引用
  • 直接引用 被源码中直接调用的库
  • 间接引用 被调用库的依赖库
  • -lxxx 指定具体的库名称,编译时需要显式指定直接引用的库名称
  • -L 指定链接库的位置,编译时需要显式指定直接引用的库位置
  • -Wl,-rpath-link ,用于编译时指定间接引用的库位置
    如果知道所有间接引用的库文件名称,并且不嫌麻烦,也可以用-lxxx显式指定每一个库(不推荐-lxxx)
  • -Wl,-rpath ,有两个作用:
    1. 用于编译时指定间接引用的库位置,作用同-Wl,-rpath-link
    2. 用于运行时指定所有引用库的位置,作用同修改环境变量(LD_LIBRARY_PATH),并且库路径引用优先级高于LD_LIBRARY_PATH
  • 使用建议
    1. 编译命令中使用-Wl,-rpath-link 指定间接引用库位置(编译时),使用-Wl,-rpath 指定引用库位置(运行时)
    2. -Wl,-rpath-link 在 -Wl,-rpath 前