zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux下库的使用--静态库

Linux静态 -- 使用
2023-09-11 14:22:20 时间

一、编辑代码如下:

#vector.h 

1 #ifndef vector_h
 2 #define vector_h
 3 
 4 void addVec(int *xP, int *yP, int *zP, int Num);
 5 void mulVec(int *xP, int *yP, int *zP, int Num);                                        
 6 
 7 #endif

#addVec.c

 1 #include "vector.h"
 2                                                                                         
 3 void addVec(int *xP, int *yP, int *zP, int Num){
 4     for(int i = 0; i < n; i++){
 5         zP[i] = xP[i] + yP[i];
 6     }
 7 
 8     return;
 9 }
~   

#mulVec.c

 1 #include "vector.h"                                                                     
 2 
 3 void mulVec(int *xP, int *yP, int *zP, int Num){
 4     for(int i = 0; i < n; i++){
 5         zP[i] = xP[i] * yP[i];
 6     }
 7 
 8     return;
 9 }


#testVec.c

 1 #include <stdio.h>                                                                      
 2 #include "vector.h"
 3 
 4 int x[2] = {1, 2};
 5 int y[2] = {3, 4};
 6 int z[2];
 7 
 8 int main(int argc, char **argv)
 9 {
10     addVec(x, y, z, 2);
11     printf("z = [%d %d]\n", z[0],  z[1]);
12 
13     return 0;
14 }

二、生成静态库文件:

  1、gcc -Og -c *Vec.c            //生成目标文件

  2、ls *.o  或  ls | grep .o             //查看目标文件

  3、ar crs libVector.a addVec.o mulVec.o     //生成静态库文件

  4、ls *.a                   //查看生成的库文件

  5、file libVector.a                //查看文件类型

  6、ar -t libVector.a              //查看文件内容

三、使用静态库文件:

  gcc testVec.o libVector.a -o testVec      //生成可执行文件

四、运行程序:

  testVec                   //运行,出结果z = [4 6]
五、查看testVec代码
  objdump -d testVec,结果:

objdump -d testVec

testVec:     文件格式 elf64-x86-64


Disassembly of section .init:

0000000000001000 <_init>:
    1000:       f3 0f 1e fa             endbr64 
    1004:       48 83 ec 08             sub    $0x8,%rsp
    1008:       48 8b 05 d9 2f 00 00    mov    0x2fd9(%rip),%rax        # 3fe8 <__gmon_start__>
    100f:       48 85 c0                test   %rax,%rax
    1012:       74 02                   je     1016 <_init+0x16>
    1014:       ff d0                   callq  *%rax
    1016:       48 83 c4 08             add    $0x8,%rsp
    101a:       c3                      retq   

Disassembly of section .plt:

0000000000001020 <.plt>:
    1020:       ff 35 e2 2f 00 00       pushq  0x2fe2(%rip)        # 4008 <_GLOBAL_OFFSET_TABLE_+0x8>
    1026:       ff 25 e4 2f 00 00       jmpq   *0x2fe4(%rip)        # 4010 <_GLOBAL_OFFSET_TABLE_+0x10>
    102c:       0f 1f 40 00             nopl   0x0(%rax)

0000000000001030 <printf@plt>:
    1030:       ff 25 e2 2f 00 00       jmpq   *0x2fe2(%rip)        # 4018 <printf@GLIBC_2.2.5>
    1036:       68 00 00 00 00          pushq  $0x0
    103b:       e9 e0 ff ff ff          jmpq   1020 <.plt>

Disassembly of section .text:

0000000000001040 <_start>:
    1040:       f3 0f 1e fa             endbr64 
    1044:       31 ed                   xor    %ebp,%ebp
    1046:       49 89 d1                mov    %rdx,%r9
    1049:       5e                      pop    %rsi
    104a:       48 89 e2                mov    %rsp,%rdx
    104d:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1051:       50                      push   %rax
    1052:       54                      push   %rsp
    1053:       4c 8d 05 c6 01 00 00    lea    0x1c6(%rip),%r8        # 1220 <__libc_csu_fini>
    105a:       48 8d 0d 4f 01 00 00    lea    0x14f(%rip),%rcx        # 11b0 <__libc_csu_init>
    1061:       48 8d 3d d1 00 00 00    lea    0xd1(%rip),%rdi        # 1139 <main>
    1068:       ff 15 72 2f 00 00       callq  *0x2f72(%rip)        # 3fe0 <__libc_start_main@GLIBC_2.2.5>
    106e:       f4                      hlt    
    106f:       90                      nop

0000000000001070 <deregister_tm_clones>:
    1070:       48 8d 3d c9 2f 00 00    lea    0x2fc9(%rip),%rdi        # 4040 <__TMC_END__>
    1077:       48 8d 05 c2 2f 00 00    lea    0x2fc2(%rip),%rax        # 4040 <__TMC_END__>
    107e:       48 39 f8                cmp    %rdi,%rax
    1081:       74 15                   je     1098 <deregister_tm_clones+0x28>
    1083:       48 8b 05 4e 2f 00 00    mov    0x2f4e(%rip),%rax        # 3fd8 <_ITM_deregisterTMCloneTable>
    108a:       48 85 c0                test   %rax,%rax
    108d:       74 09                   je     1098 <deregister_tm_clones+0x28>
    108f:       ff e0                   jmpq   *%rax
    1091:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
    1098:       c3                      retq   
    1099:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

00000000000010a0 <register_tm_clones>:
    10a0:       48 8d 3d 99 2f 00 00    lea    0x2f99(%rip),%rdi        # 4040 <__TMC_END__>
    10a7:       48 8d 35 92 2f 00 00    lea    0x2f92(%rip),%rsi        # 4040 <__TMC_END__>
    10ae:       48 29 fe                sub    %rdi,%rsi
    10b1:       48 89 f0                mov    %rsi,%rax
    10b4:       48 c1 ee 3f             shr    $0x3f,%rsi
    10b8:       48 c1 f8 03             sar    $0x3,%rax
    10bc:       48 01 c6                add    %rax,%rsi
    10bf:       48 d1 fe                sar    %rsi
    10c2:       74 14                   je     10d8 <register_tm_clones+0x38>
    10c4:       48 8b 05 25 2f 00 00    mov    0x2f25(%rip),%rax        # 3ff0 <_ITM_registerTMCloneTable>
    10cb:       48 85 c0                test   %rax,%rax
    10ce:       74 08                   je     10d8 <register_tm_clones+0x38>
    10d0:       ff e0                   jmpq   *%rax
    10d2:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
    10d8:       c3                      retq   
    10d9:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

00000000000010e0 <__do_global_dtors_aux>:
    10e0:       f3 0f 1e fa             endbr64 
    10e4:       80 3d 55 2f 00 00 00    cmpb   $0x0,0x2f55(%rip)        # 4040 <__TMC_END__>
    10eb:       75 33                   jne    1120 <__do_global_dtors_aux+0x40>
    10ed:       55                      push   %rbp
    10ee:       48 83 3d 02 2f 00 00    cmpq   $0x0,0x2f02(%rip)        # 3ff8 <__cxa_finalize@GLIBC_2.2.5>
    10f5:       00 
    10f6:       48 89 e5                mov    %rsp,%rbp
    10f9:       74 0d                   je     1108 <__do_global_dtors_aux+0x28>
    10fb:       48 8b 3d 26 2f 00 00    mov    0x2f26(%rip),%rdi        # 4028 <__dso_handle>
    1102:       ff 15 f0 2e 00 00       callq  *0x2ef0(%rip)        # 3ff8 <__cxa_finalize@GLIBC_2.2.5>
    1108:       e8 63 ff ff ff          callq  1070 <deregister_tm_clones>
    110d:       c6 05 2c 2f 00 00 01    movb   $0x1,0x2f2c(%rip)        # 4040 <__TMC_END__>
    1114:       5d                      pop    %rbp
    1115:       c3                      retq   
    1116:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
    111d:       00 00 00 
    1120:       c3                      retq   
    1121:       66 66 2e 0f 1f 84 00    data16 nopw %cs:0x0(%rax,%rax,1)
    1128:       00 00 00 00 
    112c:       0f 1f 40 00             nopl   0x0(%rax)

0000000000001130 <frame_dummy>:
    1130:       f3 0f 1e fa             endbr64 
    1134:       e9 67 ff ff ff          jmpq   10a0 <register_tm_clones>

0000000000001139 <main>:
    1139:       48 83 ec 08             sub    $0x8,%rsp
    113d:       b9 02 00 00 00          mov    $0x2,%ecx
    1142:       48 8d 15 ff 2e 00 00    lea    0x2eff(%rip),%rdx        # 4048 <z>
    1149:       48 8d 35 e0 2e 00 00    lea    0x2ee0(%rip),%rsi        # 4030 <y>
    1150:       48 8d 3d e1 2e 00 00    lea    0x2ee1(%rip),%rdi        # 4038 <x>
    1157:       e8 27 00 00 00          callq  1183 <addVec>
    115c:       8b 15 ea 2e 00 00       mov    0x2eea(%rip),%edx        # 404c <z+0x4>
    1162:       8b 35 e0 2e 00 00       mov    0x2ee0(%rip),%esi        # 4048 <z>
    1168:       48 8d 3d 95 0e 00 00    lea    0xe95(%rip),%rdi        # 2004 <_IO_stdin_used+0x4>
    116f:       b8 00 00 00 00          mov    $0x0,%eax
    1174:       e8 b7 fe ff ff          callq  1030 <printf@plt>
    1179:       b8 00 00 00 00          mov    $0x0,%eax
    117e:       48 83 c4 08             add    $0x8,%rsp
    1182:       c3                      retq   

0000000000001183 <addVec>:
    1183:       49 89 f9                mov    %rdi,%r9
    1186:       49 89 f0                mov    %rsi,%r8
    1189:       48 89 d7                mov    %rdx,%rdi
    118c:       89 ce                   mov    %ecx,%esi
    118e:       b8 00 00 00 00          mov    $0x0,%eax
    1193:       eb 11                   jmp    11a6 <addVec+0x23>
    1195:       48 63 d0                movslq %eax,%rdx
    1198:       41 8b 0c 90             mov    (%r8,%rdx,4),%ecx
    119c:       41 03 0c 91             add    (%r9,%rdx,4),%ecx
    11a0:       89 0c 97                mov    %ecx,(%rdi,%rdx,4)
    11a3:       83 c0 01                add    $0x1,%eax
    11a6:       39 f0                   cmp    %esi,%eax
    11a8:       7c eb                   jl     1195 <addVec+0x12>
    11aa:       c3                      retq   
    11ab:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)

00000000000011b0 <__libc_csu_init>:
    11b0:       f3 0f 1e fa             endbr64 
    11b4:       41 57                   push   %r15
    11b6:       4c 8d 3d 2b 2c 00 00    lea    0x2c2b(%rip),%r15        # 3de8 <__frame_dummy_init_array_entry>
    11bd:       41 56                   push   %r14
    11bf:       49 89 d6                mov    %rdx,%r14
    11c2:       41 55                   push   %r13
    11c4:       49 89 f5                mov    %rsi,%r13
    11c7:       41 54                   push   %r12
    11c9:       41 89 fc                mov    %edi,%r12d
    11cc:       55                      push   %rbp
    11cd:       48 8d 2d 1c 2c 00 00    lea    0x2c1c(%rip),%rbp        # 3df0 <__do_global_dtors_aux_fini_array_entry>
    11d4:       53                      push   %rbx
    11d5:       4c 29 fd                sub    %r15,%rbp
    11d8:       48 83 ec 08             sub    $0x8,%rsp
    11dc:       e8 1f fe ff ff          callq  1000 <_init>
    11e1:       48 c1 fd 03             sar    $0x3,%rbp
    11e5:       74 1f                   je     1206 <__libc_csu_init+0x56>
    11e7:       31 db                   xor    %ebx,%ebx
    11e9:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
    11f0:       4c 89 f2                mov    %r14,%rdx
    11f3:       4c 89 ee                mov    %r13,%rsi
    11f6:       44 89 e7                mov    %r12d,%edi
    11f9:       41 ff 14 df             callq  *(%r15,%rbx,8)
    11fd:       48 83 c3 01             add    $0x1,%rbx
    1201:       48 39 dd                cmp    %rbx,%rbp
    1204:       75 ea                   jne    11f0 <__libc_csu_init+0x40>
    1206:       48 83 c4 08             add    $0x8,%rsp
    120a:       5b                      pop    %rbx
    120b:       5d                      pop    %rbp
    120c:       41 5c                   pop    %r12
    120e:       41 5d                   pop    %r13
    1210:       41 5e                   pop    %r14
    1212:       41 5f                   pop    %r15
    1214:       c3                      retq   
    1215:       66 66 2e 0f 1f 84 00    data16 nopw %cs:0x0(%rax,%rax,1)
    121c:       00 00 00 00 

0000000000001220 <__libc_csu_fini>:
    1220:       f3 0f 1e fa             endbr64 
    1224:       c3                      retq   

Disassembly of section .fini:

0000000000001228 <_fini>:
    1228:       f3 0f 1e fa             endbr64 
    122c:       48 83 ec 08             sub    $0x8,%rsp
    1230:       48 83 c4 08             add    $0x8,%rsp
    1234:       c3                      retq 

 

  objdump -d addVec.o结果:

0000000000000000 <addVec>:
   0:   49 89 f9                mov    %rdi,%r9
   3:   49 89 f0                mov    %rsi,%r8
   6:   48 89 d7                mov    %rdx,%rdi
   9:   89 ce                   mov    %ecx,%esi
   b:   b8 00 00 00 00          mov    $0x0,%eax
  10:   eb 11                   jmp    23 <addVec+0x23>
  12:   48 63 d0                movslq %eax,%rdx
  15:   41 8b 0c 90             mov    (%r8,%rdx,4),%ecx
  19:   41 03 0c 91             add    (%r9,%rdx,4),%ecx
  1d:   89 0c 97                mov    %ecx,(%rdi,%rdx,4)
  20:   83 c0 01                add    $0x1,%eax
  23:   39 f0                   cmp    %esi,%eax
  25:   7c eb                   jl     12 <addVec+0x12>
  27:   c3                      retq

  发现addVec.o全部进入了可执行文件中,而mulVec.o则没有进入,唯一的区别就是在地址上的不同。

静态库使用中,必须将用到的所有的目标文件加进最后形成的可执行文件中,会造成文件过大的情况出现。