getopt.h: Compiling Linux C-Code in Windows

90,379

Solution 1

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!

Solution 2

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 */
}

Solution 3

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

Solution 4

I did compile the getopt code under windows.

I did this as I wanted to explicilty use its command line parsing functionality in a windows (command line) app.

I successfully did this using VC2010.

As far as I remember I ran into no significant issues doing so.

getopt.c getoptl.c

Solution 5

if you just want getopt to be used in visual c++ without other dependences, I have port the getopt.c from latest gnu libc 2.12, with all new features.The only difference is you have to use TCHAR instead of char,but This is very common in windows.

simply download the source, make, copy libgetopt.lib and getopt.h getopt_int.h to your project.

you can also make it using CMakeList.txt in the root dir.

download the source from github

Share:
90,379
john_science
Author by

john_science

Excited about the places where science and software meet. Currently working in nuclear reactor design. Eternal student. (Rock Climbing and bread baking for fun.)

Updated on July 09, 2022

Comments

  • john_science
    john_science almost 2 years

    I am trying to get a set of nine *.c files (and nine related *.h files) to compile under Windows.

    The code was originally designed in Linux to take command line arguments using the standard GNU-Linux/C library "getopt.h". And that library does not apply to building the C-code in Windows.

    I want to ignore what my code does right now and ask the following question. For those of you familiar with this C-library "getopt.h": will it be possible to build and run my code in Windows if it depends on POSIX-style command-line arguments? Or will I have to re-write the code to work for Windows, passing input files differently (and ditching the "getopt.h" dependency)?

    • Christoph
      Christoph about 12 years
      if MSVC support is not a hard requirement, there's always MinGW: I'm quite happy with the MinGW cross-compiler packages which come with Cygwin...
    • alk
      alk about 12 years
      getopt.h discribes the API provided by methods from getopt.c. Pull it from somewhere, compile it and link the result to your app and your done.
    • Admin
      Admin over 7 years
      Just a nit regarding alk's comment, it can be done, technically, but the legality depends entirely on the compatibility of the license(s) from the getopt sources (if any), and those of the project. Some getopt implementations say public domain, so that would likely be no problem.
  • john_science
    john_science about 12 years
    I will give that a try right now. But I cannot tell from the link you showed: is the API for this library the same as the old getopt API?
  • john_science
    john_science about 12 years
    Thanks! I'm not sure if this will be practical for me. While I understand I could scroll through the ~5000 lines of code and re-write these bits, this is code that we are getting from other developers. So, what happens in 2 months when they give us a slightly newer version of the code? Will I have to redo the argument processing code every time we get an update? That sounds time consuming.
  • Benj
    Benj about 12 years
    It sounds as though you need to get together with these other developers and agree an argument passing system which is portable. The solution provided by @theJollySin may well be the answer, but either way it's not difficult to write a portable argument parser.
  • alk
    alk about 12 years
    Could you name any reason to do a re-write, but doing parsing exercises?
  • alk
    alk about 12 years
    Over the time the getopt API looked quiet stable to me. @theJollySin
  • john_science
    john_science about 12 years
    Well, the code I am porting over (for use in a much larger system) is a few months old. But the people developing it are as much scientists as engineers, so they don't usually use the newest compilers or even the newest versions of UNIX/LINUX. So if they are doing things in an old-fashioned way, that wouldn't surprise me.
  • alk
    alk about 12 years
    'a few month` :-) no issues with getopt being to young or even to mature... you might very well give it a try a pull the getopt sources from the links in my answer and use them.
  • john_science
    john_science about 12 years
    @Dogbert That was almost the perfect solution. Except I am having a really annoying time trying to run the library you linked (which is C++) with the code base I currently have (which is C). I have not yet given up that this will be possible, but mixing the two language flavors seems to add some complexity to Visual Studio builds.
  • Michael Petrotta
    Michael Petrotta almost 12 years
    A suggested edit added the sentence "Please note, that this specific code is covered by the GPLv3 and not by the LGPL." to this answer. I rolled that back, and include the comment here.
  • Victor Sergienko
    Victor Sergienko over 11 years
    Er, including getopt.in.h?
  • alk
    alk over 11 years
    @VictorSergienko: Sure some code out of getopt.in.h was necessary to compile getopt's sources. Anyway, my answer was not meant to be understood the way that compiling the files linked was possible without any (minor) modifications. Just feeding them "as is" to VC10 would not work.
  • bobobobo
    bobobobo about 11 years
    Why do it manually when the getopt function is available on the internet?
  • john_science
    john_science almost 11 years
    It looks like your library would have been really helpful. I just wanted to say. But I saw Dogbert's comment about Xgetopt first and that did the trick. Thanks, though!
  • eleijonmarck
    eleijonmarck over 9 years
    I have this included in my project, however when I try to use optarg it says that it is undeclared in Windows. The return value of getopt is always the opt. How can I use optarg?
  • john_science
    john_science almost 9 years
    Well, finding getopt.h wasn't my problem. I was more worried about the Windows getopt.h working in a Linux environment. Good to know though.
  • cbr
    cbr over 7 years
    The header file is missing extern char *optarg; and extern int optind, opterr, optopt;
  • namezero
    namezero almost 4 years
    Thanks. That works copy and paste for a quick port to use a program really quick
  • Varun Garg
    Varun Garg almost 3 years
    doesn't compile on win