getopt.h: Compiling Linux C-Code in Windows getopt.h: Compiling Linux C-Code in Windows c c

getopt.h: Compiling Linux C-Code in Windows


getopt() is actually a really simple function. I made a github gist for it, code from here is below too

#include <string.h>#include <stdio.h>int     opterr = 1,             /* if error message should be printed */  optind = 1,             /* index into parent argv vector */  optopt,                 /* character checked for validity */  optreset;               /* reset getopt */char    *optarg;                /* argument associated with option */#define BADCH   (int)'?'#define BADARG  (int)':'#define EMSG    ""/** getopt --*      Parse argc/argv argument vector.*/int  getopt(int nargc, char * const nargv[], const char *ostr){  static char *place = EMSG;              /* option letter processing */  const char *oli;                        /* option letter list index */  if (optreset || !*place) {              /* update scanning pointer */    optreset = 0;    if (optind >= nargc || *(place = nargv[optind]) != '-') {      place = EMSG;      return (-1);    }    if (place[1] && *++place == '-') {      /* found "--" */      ++optind;      place = EMSG;      return (-1);    }  }                                       /* option letter okay? */  if ((optopt = (int)*place++) == (int)':' ||    !(oli = strchr(ostr, optopt))) {      /*      * if the user didn't specify '-' as an option,      * assume it means -1.      */      if (optopt == (int)'-')        return (-1);      if (!*place)        ++optind;      if (opterr && *ostr != ':')        (void)printf("illegal option -- %c\n", optopt);      return (BADCH);  }  if (*++oli != ':') {                    /* don't need argument */    optarg = NULL;    if (!*place)      ++optind;  }  else {                                  /* need an argument */    if (*place)                     /* no white space */      optarg = place;    else if (nargc <= ++optind) {   /* no arg */      place = EMSG;      if (*ostr == ':')        return (BADARG);      if (opterr)        (void)printf("option requires an argument -- %c\n", optopt);      return (BADCH);    }    else                            /* white space */      optarg = nargv[optind];    place = EMSG;    ++optind;  }  return (optopt);                        /* dump back option letter */}


You are correct. getopt() is POSIX, not Windows, you would generally have to re-write all command-line argument parsing code.

Fortunately, there is a project, Xgetopt, that is meant for Windows/MFC classes.

http://www.codeproject.com/Articles/1940/XGetopt-A-Unix-compatible-getopt-for-MFC-and-Win32

If you can get this working in your project, it should save you a fair bit of coding and prevent you from having to rework all parsing.

Additionally, it comes with a nice GUI-enabled demo app that you should find helpful.

Good luck!


There is a possibilty to use code from MinGW runtime (by Todd C. Miller):

http://sourceforge.net/apps/trac/mingw-w64/browser/trunk/mingw-w64-crt/misc

I have created a small library with these files and CMake script (can generate a VS project):

https://github.com/alex85k/wingetopt