Cannot Find “getopt.h” File When Compiling Under Windows

Often times, issues arise when compiling C/C++ code developed for Linux under Windows. One annoying problem is when the code requires some header which is only available in the POSIX API. A header commonly used for parsing the command line arguments is getopt.h. Unfortunately, this header is only available under Linux. After some digging around, I found a port of this header file for Windows here.

In case the repository went down in the future, I’ve pasted the code here. All credits go to the original author. Click on the link below for the full code.

2 comments

    • Maria on January 24, 2020 at 1:23 AM
    • Reply

    Hi Mehran,
    Thank you for sharing this helpful post. I have copied this replacement of “getopt” to my program, but I have a problem.Here, in line 79: static char *place = EMSG; , an error with code E0144 occurs which says that : a value of type “const char *” cannot be used to initialize an entity of type “char *”.
    May you help me ti fix it please?

    • Peter Olcott on February 17, 2020 at 5:46 PM
    • Reply

    Replacing line 69 with line 67 fixes the problem.

    #ifdef __CYGWIN__
    static char EMSG[] = “”;
    #else
    static char EMSG[] = “”;
    #endif

Leave a Reply

Your email address will not be published.