Linux下的空洞文件,静态库和动态库

2021/8/26 7:08:35

本文主要是介绍Linux下的空洞文件,静态库和动态库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一.空洞文件

什么是空洞文件:在UNIX文件操作系统中,文件的位移量可以大于文件的当前长度。在这种情况下,对文件的下一次写将延长该文件,并在文件中构成一个空洞,这一点是允许的。位于文件中但没有写的字节都被设为0。

创建一个空洞文件:1.以只写的方式打开该文件

                                2.移动文件指针,移动1G

                                3.在文件末尾写一个字节

                                4.关闭文件

二.Linux下操作目录的API

目录的打开:

        #include <sys/types.h>

        #include <dirent.h>

        DIR *opendir(const char *name);

        参数1: 目录名

        返回值:  成功返回DIR *,失败则返回NULL

目录的关闭:

        #include <sys/types.h>

        #include <dirent.h>

        int closedir(DIR *dirp);

目录的读取:

        #include <dirent.h>

        struct dirent *readdir(DIR *dirp);

        参数1:打开的目录指针

        返回值:成功struct dirent *,失败则返回NULL

                 struct dirent {

                               ino_t          d_ino;       /* inode number */

                               off_t          d_off;       /* offset to the next dirent */

                               unsigned short d_reclen;    /* length of this record */

                               unsigned char  d_type;      /* type of file; not supported

                                                                              by all file system types */

                               char           d_name[256]; /* filename */   --->文件名

                         };

三.文件属性API

获得文件属性:

stat/lstat/fstat 获得文件属性( fstat参数是文件描述符,不建议使用)

         #include <sys/types.h>

         #include <sys/stat.h>

         #include <unistd.h>

         int fstat(int fd, struct stat *buf);

         int stat(const char *path, struct stat *buf);

         int lstat(const char *path, struct stat *buf);

               注:  如果path是符号链接,stat获得是目标文件的属性  lstat获得的是链接文件的属性 

         参数1:文件名(绝对路径)

        参数2:  struct stat * 获得文件属性之后,将文件属性的值放到buf所指的空间

        返回值:若失败则返回-1

struct stat {

               dev_t     st_dev;     /* ID of device containing file */

               ino_t     st_ino;     /* inode number */

               mode_t    st_mode;    /* protection */

               nlink_t   st_nlink;   /* number of hard links */

               uid_t     st_uid;     /* user ID of owner */

               gid_t     st_gid;     /* group ID of owner */

               dev_t     st_rdev;    /* device ID (if special file) */

               off_t      st_size;    /* total size, in bytes */   ---->文件的大小

               blksize_t st_blksize; /* blocksize for file system I/O */

               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */

               time_t    st_atime;   /* time of last access */

               time_t    st_mtime;   /* time of last modification */

               time_t    st_ctime;   /* time of last status change */

           };

四.静态库和动态库

静态库和动态库的区别:

静态库:编译时把静态库的相关代码复制到可执行程序中

                程序运行时无需加载库,运行速度更快

                占用更多的磁盘和内存空间

                静态库升级后,需要重新编译链接

动态库:编译时仅记录用到那个共享库中的符号,不复制共享库中的相关代码

                程序不包含库中代码,体积比较小

                库升级方便,无需重新编译

                在运行时需要加载共享库

静态库的创建:

 1.编写源代码

2.生成相应的.o文件

3.创建成相应的静态库

        静态库的命名方式   lib+库名.a

        ar rcs  libfile.a  fileLine.o   filelength.o

        ar--->用来创建静态库

        r ---->在库中插入模块

        c---->创建一个库,不管库是否存在,都将创建

        s---->创建目标文件索引,这在创建较大的库时能加快时间

4.编译时链接静态库

5.运行

动态库的创建:

1.编写源文件

2.生成相应的.o文件

3.生成动态库

        动态库的命名方式:  lib+库名.so.版本号    用数字来表示版本号

        gcc -shared -o libfile.so.1  filelength.o fileLine.o

4.为共享库文件创建软连接文件

        创建链接文件的目的就是为了能够让我们扥的编译器在编译时找到共享库

        ln -s libfile.so.1 libfile.so

5.编译链接共享库,同时指明库路径

        gcc test.c -o test -L. -lfile

6.运行时需要链接动态库

7.将动态库复制到/lib/usr/lib



这篇关于Linux下的空洞文件,静态库和动态库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程