Linux gperf command and linuxgperf command

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.