Linux(目录操作及库的制作)

目录的操作
1. 打开目录
#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
参数:打开目录的路径
返回值:成功 - 目录流指针
失败 - NULL,设置errno
2. 读目录
struct dirent *readdir(DIR dirp);
参数:目录流指针 - opendir的返回值
返回值:成功 - 保存文件信息的结构体指针(NULL - 读到目录尾)
失败 - NULL, 设置errno的值
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 */
};

实现ls的功能

int main()
{
struct dirent *p = NULL;
DIR *d = opendir("/home/fs/07");
if(d == NULL){
perror("/home/fs/071");
return -1;
}
while((p = readdir(d))!= NULL){
printf("%s\n", p->d_name);
}
return 0;
}
3. 获取文件属性
#include <sys/stat.h>
int stat(const char *restrict path, struct stat restrict buf);
参数1:文件路径
参数2:保存获取到的信息的结构体
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 */
};
返回值:成功 - 0
失败 - -1,设置错误码

实现ls -l的功能
int main()
{
int ret;
struct stat s;
struct dirent *p = NULL;
struct tm *t;
DIR *d = opendir("/home/fs/07");
if(d == NULL){
perror("/home/fs/071");
return -1;
}
while((p = readdir(d))!= NULL){
stat(p->d_name, &s);
t = localtime(&s.st_ctime);
printf("%x %d %d:%d %s\n", s.st_mode, s.st_size, t->tm_hour, t->tm_min, p->d_name);
}
}

库的制作
什么是库
库是一种可执行代码的二进制形式,通常把一些常用的函数制作成各种函数库,然后被系统载入内存中运行
两种库 - 载入的时间不同
静态库:在链接阶段被加载到目标文件中
程序运行时不需要静态库
编译后的可执行程序体积较大
命名:libXXX.a
动态库:编译的时候不会被链接进来,在程序执行的时候被载入
程序运行时需要动态库
编译后的可执行程序体积不变
命名:libXXX.so

  1. 静态库的制作
    使用GNU下的ar 工具制作
    (1) 写好功能文件和头文件 - hello.c 和 hello.h
    (2) 将hello.c 编译生成目标文件 hello.o
    gcc hello.c -c -o hello.o
    (3) 使用ar 将hello.o制作成静态库
    ar crs libmyhello.a hello.o - hello.o不能放在前面
    参数:c:表示无提示方式创建文件包
    r:在文件包中替代文件
    s:强制重新生成文件包的符号表
    (4) 测试
    gcc test.c -lmyhello -L.
    -l:指定要链接的库文件
    -L:指定库文件所在的路径

  2. 制作动态库
    使用gcc工具
    (1) 写好功能文件和头文件 - hello.c 和 hello.h
    (2) 使用gcc编译生成动态库
    gcc hello.c -fpic -c -o hello.o //可以不加
    gcc hello.o -shared -o libmyhello.so
    或者:gcc hello.c -fpic -shared -o hello.o
    -shared:指定生成动态链接库
    -fPIC(-fpic):表示编译为位置独立的代码。位置独立的代码即位置无关代码,
    在可执行程序加载的时候可以存放在内存内的任何位置。若不使用该选项则编译后的代码是位置相关的代码,
    在可执行程序加载时是通过代码拷贝的方式来满足不同的进程的需要,没有实现真正意义上的位置共享

    (3) 测试
    gcc test.c -lmyhello -L.

    ldd:查看某个可执行程序加载库的情况
    注意:当静态库和动态库重名时,系统会以动态库为准

动态库的加载过程
执行可执行程序时,会加载动态库,寻找存放动态库的配置文件
存放动态库的配置文件的默认目录为 /etc/ld.so.conf.d/以及所属的众多子目录内的配置文件
配置文件指定的库文件的绝对路径为 /usr/lib或/lib
根据配置文件中的库文件的路径去往/usr/lib 或/lib,将动态库加载到可执行程序中
使动态库生效
1. 将动态库拷贝到 /usr/lib或/lib目录下
2. 修改LD_LIBRARY_PATH环境变量
sudo vim /etc/bash.bashrc
在最后添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/linux/beike/lib
重启终端才能生效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值