动态库的链接详解-linux

链接动态库

为什么需要链接?

作为编译的最后一公里和运行的刚需。

linux从程序(program或对象)变成进程(process或进程)简单的说分三步:

    1、fork进程,在内核创建进程相关内核项,加载进程可执行文件;
    2、查找依赖的so,一一加载映射虚拟地址
    3、初始化程序变量。

来看一看链接的细节:

此文环境

[root@VM_0_9_centos dynamic_lib_text]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel(R) Xeon(R) CPU E5-26xx v4
Stepping:              2
CPU MHz:               2394.454
BogoMIPS:              4788.90
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0

[root@VM_0_9_centos dynamic_lib_text]# cat add.c 


int add(int a, int b){
        return a+b;
}
[root@VM_0_9_centos dynamic_lib_text]# cat main.c 
#include<iostream>
#include"add.h"
int main(){

        int iret = add(10,29);
        std::cout<<"use dynamic library. iret="<<iret<<std::endl;


        return 0;
}
[root@VM_0_9_centos dynamic_lib_text]# 
[root@VM_0_9_centos dynamic_lib_text]# g++ -fPIC -shared -o libadd.so ./add.c
[root@VM_0_9_centos dynamic_lib_text]# 
[root@VM_0_9_centos dynamic_lib_text]# export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
[root@VM_0_9_centos dynamic_lib_text]# 

链接libadd.so

[root@VM_0_9_centos dynamic_lib_text]# g++ main.o -L . -ladd -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/
share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --
enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --
enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --
enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c+
+,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --
disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-
redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-
x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic 
--with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-
redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-
redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/root/lib/mysql_connector_c++/lib64/../lib64/:/usr/lib/gcc/x86_64-
redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/
lib/../lib64/:/usr/lib/../lib64/:/root/lib/mysql_connector_c++/lib64/:/root/lib/
mysql_connector_c/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-L.' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --
eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-
x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/
lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-
redhat-linux/4.8.5/crtbegin.o -L. -L/root/lib/mysql_connector_c++/lib64/../lib64 -L/
usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-
linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/root/lib/
mysql_connector_c++/lib64 -L/root/lib/mysql_connector_c -L/usr/lib/gcc/x86_64-
redhat-linux/4.8.5/../../.. main.o -ladd -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -
lgcc /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-
linux/4.8.5/../../../../lib64/crtn.o
[root@VM_0_9_centos dynamic_lib_text]# 

编译main.c同时链接libadd.so

[root@VM_0_9_centos dynamic_lib_text]# g++ main.c -L . -ladd -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/
share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --
enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --
enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --
enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c+
+,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --
disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-
linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-
redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
COLLECT_GCC_OPTIONS='-L.' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/cc1plus -quiet -v -D_GNU_SOURCE main.c -
 quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase main -version -o /tmp/cczOi3Zb.s
GNU C++ (GCC) version 4.8.5 20150623 (Red Hat 4.8.5-28) (x86_64-redhat-linux)
        compiled by GNU C version 4.8.5 20150623 (Red Hat 4.8.5-28), GMP version 6.0.0, MPFR version 3.1.1, MPC version 1.0.1
GGC heuri
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值