cvisual-studiobuildcommand-line-argumentsgetopt

getopt.h: Compiling Linux C-Code in Windows


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)?


Solution

  • 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!