静态库和动态库

#查看库的默认搜索路径
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

相关推荐

  1. 静态动态

    2024-04-14 07:22:03       47 阅读
  2. 静态动态

    2024-04-14 07:22:03       36 阅读
  3. windows 动态静态 介绍

    2024-04-14 07:22:03       55 阅读
  4. 静态动态的了解

    2024-04-14 07:22:03       30 阅读
  5. [xmake]构建静态动态

    2024-04-14 07:22:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-14 07:22:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-14 07:22:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-14 07:22:03       82 阅读
  4. Python语言-面向对象

    2024-04-14 07:22:03       91 阅读

热门阅读

  1. 数字图像处理—图像形状特征

    2024-04-14 07:22:03       170 阅读
  2. 在Linux中,解压和删除压缩文件的命令

    2024-04-14 07:22:03       202 阅读
  3. 算力服务器包含哪些业务?

    2024-04-14 07:22:03       56 阅读
  4. 网站如何一定程度上防止ddos和压力测试

    2024-04-14 07:22:03       38 阅读
  5. Linux nfs挂载失败处理

    2024-04-14 07:22:03       55 阅读
  6. 【C语言】命令行

    2024-04-14 07:22:03       39 阅读
  7. janus搭建

    2024-04-14 07:22:03       43 阅读
  8. libtorch中API介绍

    2024-04-14 07:22:03       148 阅读
  9. FFmpeg: 自实现ijkplayer播放器--07解复用线程设计

    2024-04-14 07:22:03       39 阅读