Linux gperf command and linuxgperf command
I. Introduction
The GNU gperf tool is a perfect hash function, you can generate a hash, hash function, and C/C ++ code for a specific set of strings provided by the user. This article describes how to use gperf to implement efficient command line processing in C/C ++ code.
Ii. Installation
Source code download
http://www.gnu.org/software/gperf/https://savannah.gnu.org/projects/gperf
User Manual
http://www.gnu.org/software/gperf/manual/gperf.htmlhttp://www.cnblogs.com/napoleon_liu/archive/2010/12/27/1918057.html
Iii. Instances
Reference
http://blog.chinaunix.net/uid-9950859-id-98839.htmlhttp://www.ibm.com/developerworks/cn/linux/l-gperf.html
Example 1: Parameter Parsing
First, compile the. gperf file. Here we use example1.gperf as an example. The content is as follows:
%{ /* 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;}
Then, run the following command to convert the. gperf file to the. c file.
gperf -t -L C example1.gperf > example1.c
Compile
gcc -g -o example1 example1.c
Run