静态库和动态库

#查看库的默认搜索路径
ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012
gcc -print-search-dirs#注意输出里有些目录是不存在的

#gcc的几个option
-static
#On systems that support dynamic linking, this overrides -pie and prevents linking with the shared libraries.
-llibrary
#Search the library named library when linking.
#The -l option is passed directly to the linker by GCC.
#The linker searches a standard list of directories for the library.
#The directories searched include several standard system directories plus any that you specify with -L.
#Static libraries are archives of object files, and have file names like liblibrary.a.
#Some targets also support shared libraries, which typically have names like liblibrary.so.
#If both static and shared libraries are found, the linker gives preference to linking with the shared library
#unless the -static option is used.
-Ldir
#如果库文件不在默认的那些路径中,需要使用-L说明在哪个地方
#Add directory dir to the list of directories to be searched for -l.
-fPIC
#If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit
#on the size of the global offset table.
#This option makes a difference on AArch64, m68k, PowerPC and SPARC.
#Position-independent code requires special support, and therefore works only on certain machines.

ldd executable_file/shared library
#print shared library dependencies.(打印某个可执行程序或共享库的,共享库依赖)
#ldd prints the shared libraries required by each program or shared library specified on the command line.
#eg.(ldd /lib64/libm.so,ldd ./main)

file file_name
#file tests each argument in an attempt to classify it.
#There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests.
#The first test that succeeds causes the file type to be printed.
#file命令的几个例子
file /lib64/libm.so
#/lib64/libm.so: symbolic link to '../../lib64/libm.so.6'
file /lib64/libm.so.6
#/lib64/libm.so.6: symbolic link to 'libm-2.17.so'
file /lib64/libm-2.17.so
#/lib64/libm-2.17.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=7615, for GNU/Linux 2.6.32, not stripped
file ./main
#./main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=bea5, not stripped
file main.c
#main.c: C source, ASCII text
file Makefile
#Makefile: makefile script, UTF-8 Unicode text
file ../log
#../log: UTF-8 Unicode text
file /usr/local
#/usr/local: directory

静态库

//add.c
int add(int a,int b){
	return a+b;
}
//sub.c
int sub(int a,int b){
	return a-b;
}
//divi.c
int divi(int a,int b){
	return a/b; 
}
//mymath.h
#ifndef _MYMATH_H_
#define _MYMATH_H_
int add(int,int);
int sub(int,int);
int divi(int,int);
#endif

//gcc -c *.c
//ar rsv libmymath.a add.o sub.o divi.o
//test.c
#include <stdio.h>
#include "mymath.h"
int main(int argc, char *argv[]){
	int a=14,b=7;
	printf("%d+%d=%d\n",a,b,add(a,b));
	printf("%d-%d=%d\n",a,b,sub(a,b));
	printf("%d/%d=%d\n",a,b,divi(a,b));
	return 0;
}
//gcc test.c libmymath.a -o test -Wall
//./test

动态库

程序执行的时候(run time)会有一个动态链接

#gcc -c *.c -fPIC
#gcc -shared -o libmymath.so add.o sub.o divi.o
#gcc test.c -o test_without_rpath -lmymath -L./
第一种执行方式
#(可以看到默认是去哪里找动态库了,将动态库文件libmymath.so复制一份到那些目录中的任意一个目录,之后程序就能运行成功了)
strace ./test_without_rpath
第二种执行方式
LD_LIBRARY_PATH=./ ./test_without_rpath
第三种执行方式
#-rpath(含义看man手册,或者ld --help)是ld(GNU linker)的option,gcc使用时需要加上-Wl,
#通过这两个命令(objdump -p test_with_rpath)(readelf -a test_with_rpath)都可以看到,RPATH被硬编码到了dynamic section那里.
#如果编译时再传给ld一个--enable-new-dtags选项,则被硬编码到ELF中的就是RUNPATH.
#下面这两个编译出来,都能直接运行
gcc test.c -o test_with_rpath -lmymath -L./ -Wl,-rpath=./
gcc test.c -o test_with_rpath1 -lmymath -L./ -Wl,-rpath=./ -Wl,--enable-new-dtags

动态库的名字

#ls /usr/lib64/libm{.*,-*}
/usr/lib64/libm-2.17.so  /usr/lib64/libm.so  /usr/lib64/libm.so.6
#这三者的关系是
/usr/lib64/libm.so->/usr/lib64/libm.so.6->/usr/lib64/libm-2.17.so
#分别是linkername,soname,realname

https://man7.org/linux/man-pages/man1/ar.1.html
https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Link-Options.html
https://man7.org/conf/lca2006/shared_libraries/slide3a.html
https://cplusplus.com/forum/unices/281440/
https://man7.org/training/download/shlib_dynlinker_slides.pdf
https://www.cs.dartmouth.edu/~campbell/cs50/buildlib.html
https://man7.org/linux/man-pages/man8/ld.so.8.html
https://man7.org/linux/man-pages/man1/ld.1.html
https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
https://stackoverflow.com/questions/67131565/how-do-i-set-dt-rpath-or-dt-runpath

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值