Linux基础知识的学习(三)

1、使用外部函数库

  GCC常常与包含标准例程的外部软件库结合使用,几乎每一个 Linux 应用程序都依赖于由 GNU C函数库 GLIBC。

       头文件的使用且GCC编译时使用数学库参数。函数库位置随系统的不同而不同,但它一般会位于目录/lib或/usr/lib中,在这些目录中还有数以百计的其他必需的系统函数库。

       编译:gcc -o sin -lm sin.c

       运行:./sin


2、GNU GDB

GNU调试器是一个功能非常强大的工具。要了解更加详尽的知识,建议阅读GNU调试器手册。
调试:
        在17行(for循环处)设置断点,使用命令“b 17”。

查看断点设置情况,使用命令“info b”。

运行代码,使用命令“r”。

单步运行代码,使用命令“n”。

查看暂停点变量值,使用命令“p string2[size - i]”。

  继续程序的运行,使用命令“c”。
退出Gdb,使用命令q。


3、linux下遍历目录树

一种是用数据结构直接编一个程序(虚拟遍历)。

一种是真正遍历:

在“/”下,建文件及文件夹。

#include <unistd.h>  
#include <stdio.h>  
#include <dirent.h>  
#include <string.h>  
#include <sys/stat.h>  
void printdir(char *dir, int depth) {  
   		DIR *dp;  
   		struct dirent *entry;  
   		struct stat statbuf;  
   		if((dp = opendir(dir)) == NULL) {  
       		fprintf(stderr,"cannot open directory: %s\n", dir);  
      			return;  
    	}  
    	chdir(dir);  
    	while((entry = readdir(dp)) != NULL) {  
        	lstat(entry->d_name,&statbuf);  
        	if(S_ISDIR(statbuf.st_mode)) {  
            //Found a directory, but ignore . and ..  
            	if(strcmp(".",entry->d_name) == 0 ||   
                	strcmp("..",entry->d_name) == 0)  
                	continue;  
            	if(strcmp("dirC",entry->d_name) == 0 ||   
                	strcmp("dirA",entry->d_name) == 0|| 
					strcmp("dirB",entry->d_name) == 0){
printf("%*s%s\n",depth,"",entry->d_name);  
            		// Recurse at a new indent level  
            		printdir(entry->d_name,depth+4);  
}
        	} 
			else if(strcmp("my1.dat",entry->d_name) == 0|| 
					strcmp("my2.dat",entry->d_name) == 0 ||   
                	strcmp("my3.dat",entry->d_name) == 0)
				printf("%*s%s\n",depth,"",entry->d_name);
    	}  
    	chdir("..");  
    	closedir(dp);  
}  
int main(int argc, char* argv[]){  
    char *topdir, pwd[2]=".";  
  	    if (argc != 2)  
        	topdir=pwd;  
    	else  
        	topdir=argv[1];  
    	printf("/\n ");  
    	printdir(topdir,4);  
    	exit(0);  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值