第一次自己生成静态库

来源:互联网 发布:研究院与研究生院 知乎 编辑:程序博客网 时间:2024/06/05 21:15

只有一个别人的静态库,要实现静态库的功能,就是不知道人家内部是咋个实现的。

nm只能看到人家的几个封装好的函数,人家到底是自己实现的呢,还是调用第三方库呢?


如果是调用了第三方库,那么就应该可以看到第三方库中的函数。因为第三方库提供的是头文件,作为外部符号出现。

现在看不到符号,如果确实用了第三方库,那么就是把自己的代码放在了第三方库的源码中,封装了第三方库出来,



root@Ubuntu32:/home/zhangbin/Downloads# cd ..

root@Ubuntu32:/home/zhangbin# ls
android                Documents         mp4             STM
codeStore              Downloads         Music           streaming
Desktop                examples.desktop  Pictures        Templates
doBuildAndroidVLc.sh   ffmpeg            Public          Videos
doBuildAndroidVLc.sh~  gstreamer         Source Insight  vlc
root@Ubuntu32:/home/zhangbin# cd codeStore/
root@Ubuntu32:/home/zhangbin/codeStore# cd testCODE/
root@Ubuntu32:/home/zhangbin/codeStore/testCODE# ls
testAndroidCode  testAndroidVLC  testDEPHOME  testLibcode  testStaticLib
root@Ubuntu32:/home/zhangbin/codeStore/testCODE# cd testStaticLib/
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
main.c  main.c~  test.c  test.c~  test.h  test.h~
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o test test.c
test.c: In function ‘zhangbinTEST’:
test.c:9:7: error: expected ‘:’ or ‘...’ before ‘;’ token
test.c:11:5: error: break statement not within loop or switch
test.c:12:1: error: case label not within a switch statement
test.c:14:6: error: break statement not within loop or switch
test.c:15:1: error: ‘default’ label not within a switch statement
test.c:17:5: error: break statement not within loop or switch
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o test test.c
test.c: In function ‘zhangbinTEST’:
test.c:9:7: error: expected ‘:’ or ‘...’ before ‘;’ token
test.c:11:5: error: break statement not within loop or switch
test.c:12:1: error: case label not within a switch statement
test.c:14:6: error: break statement not within loop or switch
test.c:15:1: error: ‘default’ label not within a switch statement
test.c:17:5: error: break statement not within loop or switch
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o test test.c
test.c: In function ‘zhangbinTEST’:
test.c:10:8: error: expected ‘:’ or ‘...’ before ‘;’ token
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o test test.c
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'  可执行文件必须要有main
collect2: ld returned 1 exit status
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o test.o -c test.c
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ar libtest.a test.o
ar: invalid option -- 'e'
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV] [--plugin <name>] [member-name] [count] archive-file file...
       ar -M [<mri-script]
 commands:
  d            - delete file(s) from the archive
  m[ab]        - move file(s) in the archive
  p            - print file(s) found in the archive
  q[f]         - quick append file(s) to the archive
  r[ab][f][u]  - replace existing or insert new file(s) into the archive
  s            - act as ranlib
  t            - display contents of archive
  x[o]         - extract file(s) from the archive
 command specific modifiers:
  [a]          - put file(s) after [member-name]
  [b]          - put file(s) before [member-name] (same as [i])
  [D]          - use zero for timestamps and uids/gids
  [N]          - use instance [count] of name
  [f]          - truncate inserted file names
  [P]          - use full path names when matching
  [o]          - preserve original dates
  [u]          - only replace files that are newer than current archive contents
 generic modifiers:
  [c]          - do not warn if the library had to be created
  [s]          - create an archive index (cf. ranlib)
  [S]          - do not build a symbol table
  [T]          - make a thin archive
  [v]          - be verbose
  [V]          - display the version number
  @<file>      - read options from <file>
  --target=BFDNAME - specify the target object format as BFDNAME
 optional:
  --plugin <p> - load the specified plugin
 emulation options: 
  No emulation specific options

ar: supported targets: elf32-i386 a.out-i386-linux pei-i386 elf32-little elf32-big elf64-x86-64 elf32-x86-64 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big plugin srec symbolsrec verilog tekhex binary ihex trad-core


编译为o,不需要链接库。

ar 一些o为静态库a,也不需要链接库。



root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ar cqs libtest.a test.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
libtest.a  main.c  main.c~  test.c  test.c~  test.h  test.h~  test.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o do.o -c do.c   do引用了 test中的函数
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o       main.c   test.c   test.h   test.o
do.c~  do.h~  libtest.a  main.c~  test.c~  test.h~
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ar cqs libdo.a do.o  do引用了 test中的函数
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o     libtest.a  main.c~  test.c~  test.h~
do.c~  do.h~  libdo.a  main.c     test.c   test.h   test.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o     libtest.a  main.c~  test.c~  test.h~
do.c~  do.h~  libdo.a  main.c     test.c   test.h   test.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main.o -c main.c main引用了do里的函数。
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
main.c:12:5: error: too few arguments to function ‘memcpy’
main.c: In function ‘choseOutput’:
main.c:18:1: error: expected declaration specifiers before ‘}’ token
main.c:18:1: error: expected ‘{’ at end of input
main.c: In function ‘main’:
main.c:18:1: error: expected declaration or statement at end of input
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# man memcpy
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main.o -c main.c
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
main.c: In function ‘choseOutput’:
main.c:18:1: error: expected declaration specifiers before ‘}’ token
main.c:18:1: error: expected ‘{’ at end of input
main.c: In function ‘main’:
main.c:18:1: error: expected declaration or statement at end of input
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main.o -c main.c
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
main.c:16:17: error: expected expression before ‘int’
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main.o -c main.c
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
main.c:16:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
main.c:16:5: note: use option -std=c99 or -std=gnu99 to compile your code
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main.o -c main.c
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# man memcpy
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o     libtest.a  main.c~  test.c   test.h   test.o
do.c~  do.h~  libdo.a  main.c     main.o   test.c~  test.h~
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm libtest.a 


test.o:
         U puts
00000000 T zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm libdo.a 


do.o:
00000000 T choseOutput
         U printf
         U zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ar cqs main.a main.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm libmain.a
nm: 'libmain.a': No such file
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm main.a


main.o:
         U choseOutput
00000000 T main
         U malloc
         U printf
         U puts
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -A main.a
main.a:main.o:         U choseOutput
main.a:main.o:00000000 T main
main.a:main.o:         U malloc
main.a:main.o:         U printf
main.a:main.o:         U puts
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -g libdo.a 
do.o:
00000000 T choseOutput
         U printf
         U zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -A libtest.a libtest.a:test.o:         U puts
libtest.a:test.o:00000000 T zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -G libtest.a 
nm: invalid option -- 'G'
Usage: nm [option(s)] [file(s)]
 List symbols in [file(s)] (a.out by default).
 The options are:
  -a, --debug-syms       Display debugger-only symbols
  -A, --print-file-name  Print name of the input file before every symbol
  -B                     Same as --format=bsd
  -C, --demangle[=STYLE] Decode low-level symbol names into user-level names
                          The STYLE, if specified, can be `auto' (the default),
                          `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'
                          or `gnat'
      --no-demangle      Do not demangle low-level symbol names
  -D, --dynamic          Display dynamic symbols instead of normal symbols
      --defined-only     Display only defined symbols
  -e                     (ignored)
  -f, --format=FORMAT    Use the output format FORMAT.  FORMAT can be `bsd',
                           `sysv' or `posix'.  The default is `bsd'
  -g, --extern-only      Display only external symbols
  -l, --line-numbers     Use debugging information to find a filename and
                           line number for each symbol
  -n, --numeric-sort     Sort symbols numerically by address
  -o                     Same as -A
  -p, --no-sort          Do not sort the symbols
  -P, --portability      Same as --format=posix
  -r, --reverse-sort     Reverse the sense of the sort
      --plugin NAME      Load the specified plugin
  -S, --print-size       Print size of defined symbols
  -s, --print-armap      Include index for symbols from archive members
      --size-sort        Sort symbols by size
      --special-syms     Include special symbols in the output
      --synthetic        Display synthetic symbols as well
  -t, --radix=RADIX      Use RADIX for printing symbol values
      --target=BFDNAME   Specify the target object format as BFDNAME
  -u, --undefined-only   Display only undefined symbols
  -X 32_64               (ignored)
  @FILE                  Read options from FILE
  -h, --help             Display this information
  -V, --version          Display this program's version number


nm: supported targets: elf32-i386 a.out-i386-linux pei-i386 elf32-little elf32-big elf64-x86-64 elf32-x86-64 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big plugin srec symbolsrec verilog tekhex binary ihex trad-core
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -g libtest.a 


test.o:
         U puts
00000000 T zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -A libtest.a 
libtest.a:test.o:         U puts
libtest.a:test.o:00000000 T zhangbinTEST  本地符号
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -A libdo.a 
libdo.a:do.o:00000000 T choseOutput
libdo.a:do.o:         U printf
libdo.a:do.o:         UzhangbinTEST    外部库里的函数
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -g libdo.a 


do.o:
00000000 T choseOutput
         U printf
         U zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o     libtest.a  main.c   main.o  test.c~  test.h~
do.c~  do.h~  libdo.a  main.a     main.c~  test.c  test.h   test.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# rm main.a
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ar cqs libmain.a main.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o     libmain.a  main.c   main.o  test.c~  test.h~
do.c~  do.h~  libdo.a  libtest.a  main.c~  test.c  test.h   test.o
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main main.c
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
/tmp/ccsZXhOQ.o: In function `main':
main.c:(.text+0x98): undefined reference to `choseOutput'
collect2: ld returned 1 exit status
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main main.c -ldo
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
/usr/bin/ld: cannot find -ldo
collect2: ld returned 1 exit status
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main main.c -L. -ldo
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
./libdo.a(do.o): In function `choseOutput':
do.c:(.text+0x1a): undefined reference to `zhangbinTEST'

collect2: ld returned 1 exit status


链接成可执行文件时,需要这些库。

root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o main main.c -L. -ldo -ltest
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]

root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# 


参考 http://blog.163.com/xychenbaihu@yeah/blog/static/13222965520101023104745738/

=================



root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ar cqs libuse.amain.o do.o test.o

root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h   do.o     libmain.a  libuse.a  main.c   main.o  test.c~  test.h~
do.c~  do.h~  libdo.a  libtest.a  main      main.c~  test.c  test.h   test.o


只有main函数是本地的。其他都是外部的。外部的符号都会暴露出来的。
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# nm -A libuse.a
libuse.a:main.o:         U choseOutput          这是因为一开始是个引用的头文件吧,头文件里头有函数的声明。
libuse.a:main.o:00000000 T main  其实main函数中里还有memcpy memset,不知道为啥都没看到。

libuse.a:main.o:         U malloc
libuse.a:main.o:         U printf
libuse.a:main.o:         U puts   其实这个函数,我一直不知道哪里用到了,难道是printf???
libuse.a:do.o:00000000 T choseOutput
libuse.a:do.o:         U printf
libuse.a:do.o:         U zhangbinTEST
libuse.a:test.o:         U puts
libuse.a:test.o:00000000 T zhangbinTEST
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# 



使用多个.o放一起的新库来链接成可执行文件。

root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# gcc -o mainuse main.c -L. -luse
main.c: In function ‘main’:
main.c:10:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
main.c:12:5: warning: incompatible implicit declaration of built-in function ‘memcpy’ [enabled by default]
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ls
do.c   do.h~    libmain.a  main     main.o   test.c~  test.o
do.c~  do.o     libtest.a  main.c   mainuse  test.h
do.h   libdo.a  libuse.a   main.c~  test.c   test.h~
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# 




root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ./main
test static library 
str is zhangbin 
call static library libdo.a,libdo.a call libtest.a 
choseOutput depends on libtest.a default 
choseOutput depends on libtest.a zhang test 
choseOutput depends on libtest.a bin test 
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# ./mainuse
test static library 
str is zhangbin 
call static library libdo.a,libdo.a call libtest.a 
choseOutput depends on libtest.a default 
choseOutput depends on libtest.a zhang test 
choseOutput depends on libtest.a bin test 
root@Ubuntu32:/home/zhangbin/codeStore/testCODE/testStaticLib# 







原创粉丝点击