Search code examples
cparameter-passinggetoptansi-c

getopt() in ANSI C


I am working on OPNET and for that, I need the windows equivalent getopt() function in ANSI C language.

I need to call getopt() similar like :

while ((opt = getopt(argc, argv, "hadp:s")) != -1) {
    switch (opt) {
        case 'a':
            break;
        case 'd':
            dummy = 1;
            break;
        case 'p':
            res = atoi(optarg);
            if (1 <= res )
                break;
            else
                usage(argv[0]);
                break;
        case 's':
            use_decimal = 0;
            break;
        case 'h':
        default:
            usage(argv[0]);
    }
}

Now, when I compile, I am getting error that getopt() is undefined. I defined its header file but it can not be included in OPNET. So, I wanted to ask that how should I overcome this problem.


Solution

  • getopt() is not available in Windows. You can compile it though. Download the files from http://svn.savannah.gnu.org/svn/acct/trunk/lib and compile.

    Here someone has compiled it in VS2010, So I think you wont face any problem.