狐狸梦见乌鸦

当坚持成为一种习惯,目标将不在遥远```
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

静态库和动态库的创建

Posted on 2011-12-31 07:34  灬啊U  阅读(232)  评论(0编辑  收藏  举报

12月30号

库 一种可执行的二进制文件。。。。。

1.编译的步骤:――――>预处理(-E  .i)―――――>编译(.s)―――――>汇编(.o)――――――>链接(a.out)

2.如何创建一个静态库(static  library)。。。。。。????

①gcc  -c  hello.c       生成hello.o文件

②ar  crs  libhello.a  hello.o   生成libhello.o 静态库

③gcc  -o  a.out  main.c  -L.  –lhello   

注意:第3步中。main.c  中必须包含hello.h的头文件

                     -L 是搜索路径,. 代表当前路径。。。。。可以任意放库文件的路径

 

库名和文件名:

例: libhello.a为文件名  hello 为库名。。。。。。。

 

3.创建动态链接库(shared   library )  (fPIC 位置无关代码····)

①gcc  -fPIC  -c   hello.c        生成hello.o

②gcc  -shared  -o  libhello.so.1  hello.o        生成 libhello.so.1 

③创建一个软链接   ln  -s  libhello.so.1  libhello.so

④ {

加载库名》》》》

①     cp  libhello.so  /lib/                  将libhello.so拷贝到/lib目录下

         ②export  LD_LIBRARY_PATH=.      告诉shell库的搜索路径,导出环境变量export  LD_LIBRARY_PATH=.

}

⑤gcc  main.c  -o  a.out  -L.  –lhello

⑥运行  ./a.out  -lhello

 

4.静态库和动态库的对比:

静态库生成libhello.a文件名,库名叫hello 库中相关函数的代码复制到程序里,程序尺寸会变大,程序执行时不需要库

动态库:会做符合处理,有个跳转语句,不需要函数代码,只需函数的入口地址

尺寸小,运行时需要库

 

 

 

课程实现  ls  -l   命令的例程

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <time.h>

#include <grp.h>

#include <pwd.h>

#include <dirent.h>

 

void display_file(char *path, char *fname)

{

    struct stat buf;

    struct tm *tp;

    int n;

 

    lstat(path, &buf);

 

    switch(buf.st_mode & S_IFMT)

    {

    case S_IFBLK:   printf("b"); break;

    case S_IFCHR:   printf("c"); break;

    case S_IFDIR:   printf("d"); break;

    case S_IFREG:   printf("-"); break;

    case S_IFLNK:   printf("l"); break;

    case S_IFSOCK:  printf("s"); break;

    case S_IFIFO:   printf("p"); break;

    }

    for(n=8; n>=0; n--)

    {

        if(buf.st_mode & (1 << n))

        {

            switch(n%3)

            {

            case 2: printf("r"); break;

            case 1: printf("w"); break;

            case 0: printf("x"); break;        

            }

        }

        else

            printf("-");

    }

    printf("   %02d ", buf.st_nlink);

 

    printf("%s ", getpwuid(buf.st_uid)->pw_name);

    printf("%s ", getgrgid(buf.st_gid)->gr_name);

    printf("%8ld  ", buf.st_size);

   

    tp = localtime(&buf.st_mtime);

 

    printf("%d-%2d-%2d  %02d:%02d:%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday,\

                            tp->tm_hour, tp->tm_min, tp->tm_sec);

    printf(" %s\n", fname);

 

    return;

}

 

int main(int argc, char *argv[])

{

 

    DIR * dir;

    struct dirent *dirp;

    struct stat buf;

    char path[256];

 

    if(argc != 2)

    {

        printf("Usage : %s <dir/file>\n", argv[0]);

        return -1;

    }

 

 

    lstat(argv[1], &buf);

    if(S_ISDIR(buf.st_mode))

    {

 

        if((dir = opendir(argv[1])) == NULL)

        {

            perror("fail to opendir");

            return -1;

        }

 

        while((dirp = readdir(dir)) != NULL)

        {

            if(dirp->d_name[0] == '.')  continue;

 

            sprintf(path, "%s/%s", argv[1], dirp->d_name);

            display_file(path, dirp->d_name);

 

        }

    }

    else

    {

        display_file(argv[1], argv[1]);

    }

 

        return 0;

}