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);
}