静态链接库

1525阅读 0评论2011-04-27 onezeroone
分类:系统运维

参考:

  1. NAME
  2.        ar - create, modify, and extract from archives

       d   Delete modules from the archive.  Specify the names of modules to be deleted as member...; the
           archive is untouched if you specify no files to delete.

       p   Print the specified members of the archive, to the standard output file.

       r   Insert the files member... into archive (with replacement).

       t   Display a table listing the contents of archive, or those of the files listed in member... that
           are present in the archive.

       s   Write an object-file index into the archive, or update an existing one, even if no other change
           is made to the archive.

       v   This modifier requests the verbose version of an operation.
print1.c
  1. void
  2. print1(void)
  3. {
  4.         printf("First print\n");
  5. }

print2.c
  1. void
  2. print2(void)
  3. {
  4.         printf("Second print\n");
  5. }

create static lib file:   lib[name].a
  1. gcc -c print1.c print2.c
  2. ar -rsv libprint.a print1.o print2.o

test.c
  1. #include <stdio.h>

  2. int
  3. main(void)
  4. {
  5.         print1();
  6.         print2();

  7.         return 0;
  8. }

use:
  1. gcc -o test test.c -L./ -lprint

  1. -llibrary
  2.       
  3.            Search the library named library when linking.
  4.           
  5.            The linker searches a standard list of directories for the library, which is actually a file
               named liblibrary.a.  The linker then uses this file as if it had been specified precisely by
               name.

               The directories searched include several standard system directories plus any that you specify
               with -L


上一篇:大小端问题
下一篇:动态链接库