目录操作和库的使用

打开目录

#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd); 使用文件描述符,要配合open函数使用
DIR是用来描述一个打开的目录文件的结构体类型
成功时返回目录流指针;出错时返回NULL

读取目录

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

struct dirent是用来描述目录流中一个目录项的结构体类型
包含成员char d_name[256] 参考帮助文档
成功时返回目录流dirp中下一个目录项;
出错或到末尾时时返回NULL

#include <dirent.h>
#include <stdio.h>
int main(int argc,char **argv){

    DIR* dp;
    struct dirent *dt;
    dp=opendir("/mnt/hgfs/share/newIOP/");
    if(dp<0){
        perror("opendir");
        return 0;
    }
    
    while((dt=readdir(dp))!=NULL){
       printf("%s\n",dt->d_name);
    }
    

    closedir(dp);



}

关闭目录

closedir函数用来关闭一个目录文件:
#include <dirent.h>
int closedir(DIR *dirp);

成功时返回0;出错时返回EOF

修改文件权限

chmod/fchmod函数用来修改文件的访问权限:
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

成功时返回0;出错时返回EOF
注意:在vmware和windows共享的文件夹下,有些权限不能改变。

#include <stdio.h>
#include <sys/stat.h>

int main(int argc,char **argv){
   int ret;
   ret = chmod("temp",0444);
   if(ret<0){
        perror("chmod");
        return 0;
    }

}

获取文件属性

#include <sys/stat.h>
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);

成功时返回0;出错时返回EOF
如果path是符号链接stat获取的是目标文件的属性;而lstat获取的是链接文件的属性

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

int main (int argc,char **argv){

   struct stat buf;
   int ret;
   ret = stat("chmod_t.c",&buf);
   if(ret<0){
      perror("stat");
      return 0;

   }
   if(S_ISREG(buf.st_mode)){
       printf("-");
   }
   if(S_ISDIR(buf.st_mode)){
       printf("d");
   }
   if(S_ISCHR(buf.st_mode)){
       printf("c");
   }
   if(S_ISBLK(buf.st_mode)){
       printf("b");
   }
   if(S_ISFIFO(buf.st_mode)){
       printf("p");
   }
   if(S_ISSOCK(buf.st_mode)){
       printf("s");
   }
   
//   printf(" ");
   int i;
   for(i=8;i>=0;i--){
       if(buf.st_mode & (1<<i)){
          switch(i%3){
          case 2:
              printf("r");
              break;
          case 1:
              printf("w");
              break;
          case 0:
              printf("x");
              break;
          }
       }else{
           printf("-");
       }

   }
   
   printf(" %d",(int)buf.st_size);

   struct tm *t;
   t = localtime(&buf.st_ctime);
   printf(" %d-%d-%d %d:%d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);

   printf(" chmod_t.c\n");

}

库的概念:

库是一个二进制文件,包含的代码可被程序调用
例如:标准C库,数学库,线程库等
库有源码,可下载后编译;也可以直接安装二进制包
库文件一般放在 /lib 或 /usr/lib 下。
库的知识
库是事先编译好的,可以复用的代码。
在OS 上运行的程序基本上都要使用库,使用库可以提高开发效率。
windos和linux下库文件的格式不兼容
在linux下包含了静态库和共享库
静态库特点
编译(链接)时把静态库中相关代码复制到可执行文件中
优点:
程序中已包含代码,运行时不再需要静态库
程序运行时无需加载库,运行速度更快
缺点:
占用更多磁盘和内存空间
静态库升级后,程序需要重新编译链接

静态库

创建静态库步骤:
1 . 编写库文件代码,编译为.o 目标文件。
2. ar 命令 创建 libxxxx.a 文件
ar -rsv libxxxx.a xxxx.o

注意
1 静态库名字要以lib开头,后缀名为.a
2 没有main函数的.c 文件不能生成可执行文件。

创建静态库1:
确定库中函数的功能、接口
编写库源码hello.c

#include <stdio.h>
void hello(void){
	printf("hello world\n");
	return;
}

编译生成目标文件

$gcc -c hello.c -Wall

创建静态库2:
创建静态库hello

$ar -rsv libhello.a hello.o

创建静态库3:
ar 参数:
c 禁止在创建库时产生的正常消息
r 如果指定的文件已经存在于库中,则替换它
s 无论ar命令是否修改了库内容都强制重新生成库符号表
v 将建立新库的详细的逐个文件的描述写至标准输出
q 将指定的文件添加到库的末尾
t 将库的目录写至标准输出

创建静态库4:
查看库中符号信息

$nm libhello.a

hello.o:
0000000 T hello
        U puts
nm:显示指定文件中的符号信息
-a显示所有符号

链接错误:
test.c:(.text+0x15):对‘hello’未定义的引用
collect2: error: ld returned 1 exit status
含义:表示hello函数在编译的源码内没有找到实现
解决:实现代码或者找到对应函数的库并且链接它。

链接静态库:

gcc -o 目标文件 源码.c -L路径 -lxxxx

-L 表示库所在的路径
-l 后面跟库的名称

编写应用程序test.c

#include <stdio.h>
void hello(void);
int main(){
	hello();
	return 0;
}

编译test.c并链接静态库libhello.a

$gcc -o test test.c -L. -lhello
$./test
hello world

共享库(动态库)特点
编译(链接)时仅记录用到哪个共享库中的哪个符号,不复制共享库中相关代码
优点:
程序不包含库中代码,尺寸小
多个程序可共享同一个库
程序运行时需要加载库
库升级方便,无需重新编译程序
使用更加广泛

共享库创建1:
确定库中函数的功能、接口
编写库源代码hello.c bye.c

#include <stdio.h>
void hello(void){
	printf("hello world\n");
	return;
}

编译生成目标文件

$ gcc -c -fPIC hello.c bye.c -Wall

共享库创建2:
创建共享库common

$ gcc -shared -o libcommon.so.1 hello.o bye.o

为共享库文件创建链接文件

$ ln -s libcommon.so.1 libcommon.so

符号链接文件命名规则

lib<库名>.so

链接共享库
编写应用程序test.c

#include <stdio.h>
#include "common.h"
int main(){
	hello();
	bye();
	return 0;
}

编译test.c并链接共享库libcommon.so

$ gcc -o test test.c -L. -lcommon

加载共享库
执行程序

$ ./test
./test: error whhile loading shared libraries: libcommon.so
cannot open shared object file : No such file or directory

添加共享库的加载路径

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
$ ./test
hello world
bye!

如何找到共享库
为了让系统能找到要加载的共享库,有三种方法:
把库拷贝到/usr/lib和/lib目录下
在LD_LIBRARY_PATH环境变量中添加库所在路径
添加/etc/ld.so.conf.d/*.conf文件,执行ldconfig刷新

动态库的生成步骤:

  1. 生成位置无关代码的目标文件
    gcc -c -fPIC xxx.c xxxx.c …

  2. 生成动态库
    gcc -shared -o libxxxx.so xxx.o xxx.o …

  3. 编译可执行文件
    gcc -o 目标文件 源码.c -L路径 -lxxxx

执行动态库的可执行文件错误
./test: error while loading shared libraries: libmyheby.so: cannot open shared object file: No such file or directory

含义:可执行文件所使用的动态库找不到
解决办法:
找到动态库,添加到/usr/lib里面
或者使用export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:你的动态库目录
添加在~/.bashrc 文件里面
使用source ~/.bashrc 生效。

查看可执行文件使用的动态库:
ldd 命令 : ldd 你的可执行文件

root@haas-virtual-machine:/mnt/hgfs/share/newIOP# ldd test
	linux-vdso.so.1 =>  (0x00007fff6548d000)
	libmyheby.so => /mnt/hgfs/share/newIOP/day5/libmyheby.so (0x00007f5c89521000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5c89144000)
	/lib64/ld-linux-x86-64.so.2 (0x000055fe52211000)

出错时

root@haas-virtual-machine:/mnt/hgfs/share/newIOP/day5# ldd test
	linux-vdso.so.1 =>  (0x00007ffcb652c000)
	libmyheby.so => not found
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbeeffaf000)
	/lib64/ld-linux-x86-64.so.2 (0x0000561003c3b000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值