Linux gperf命令

一、简介

GNU 的 gperf 工具是一种 “完美的” 散列函数,可以为用户提供的一组特定字符串生成散列表、散列函数和查找函数的 C/C++ 代码。通过本文学习如何使用 gperf 实现 C/C++ 代码中高效的命令行处理。

二、安装

源码下载

用户手册

三、实例

参考

示例1:参数解析

首先,编写.gperf 文件,此处以example1.gperf为例,内容如下

%{
/* C code that goes verbatim in output */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
struct tl{ const char* name ; const char s2;};
%%
"--name",'n'
"--love",'l'
%% int main(int argc,char **argv)
{
const struct tl * str2;
int i;
char *test; for(i=1; i<argc; i++)
{ if((str2 = in_word_set(argv[i],strlen(argv[i]))) != 0)
{
switch (str2->s2)
{
case 'n':
test=argv[i+1];
printf("My name is %s.\n",test);
i++;
break;
case 'l':
printf("successed !\n");
break;
}
}
} return 0;
}

然后,执行如下指令,将.gperf 文件转换为.c文件

gperf -t -L C example1.gperf > example1.c
编译
gcc -g -o example1 example1.c

运行

Linux gperf命令

上一篇:inline、block与inline-block


下一篇:5.2 Array类型介绍