- Published on
2020-10-28-link-local-so
- Authors
- Name
- wellsleep (Liu Zheng)
title: ' Linux 下链接自定义路径动态库' Date: 2020-10-28 14:13
有的时候不希望二进制代码使用 Linux 系统路径下的动态库,如:
gcc gcrypt_linux.c -O2 -Wall -o gcrypt_target -L. -lgcrypt
生成的目标文件,其依赖的 libgcrypto.so.20
的路径为
ubuntu@ubuntu:~/ecdsa_timing$ ldd gcrypt_target
linux-vdso.so.1 (0x0000007f86128000)
libgcrypt.so.20 => /lib/aarch64-linux-gnu/libgcrypt.so.20 (0x0000007f86050000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007f85ef6000)
/lib/ld-linux-aarch64.so.1 (0x0000005572685000)
libgpg-error.so.0 => /lib/aarch64-linux-gnu/libgpg-error.so.0 (0x0000007f85ed4000)
如果要使用自己编译的动态库,需要在编译时指定新的搜索路径,如:
gcc gcrypt_linux.c -O2 -Wall -o gcrypt_target -L. -lgcrypt -Wl,-rpath,'$ORIGIN'
如果写成 Makefile 还要注意转义符的问题($符号):
all:
gcc gcrypt_linux.c -O2 -Wall -o gcrypt_target -L. -lgcrypt -Wl,-rpath,'$$ORIGIN'
编译完成后,查看依赖验证
ubuntu@ubuntu:~/ecdsa_timing$ ldd gcrypt_target
linux-vdso.so.1 (0x0000007f819a7000)
libgcrypt.so.20 => /home/ubuntu/ecdsa_timing/./libgcrypt.so.20 (0x0000007f818d6000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007f81775000)
/lib/ld-linux-aarch64.so.1 (0x000000555abda000)
libgpg-error.so.0 => /lib/aarch64-linux-gnu/libgpg-error.so.0 (0x0000007f81753000)
Done!