getopt.h:在Windows中编译Linux C代码

我试图在Windows下编译一组九个* .c文件(以及九个相关的* .h文件)。

该代码最初是在Linux中设计的,用于使用标准GNU-Linux / C库“getopt.h”获取命令行参数。 该库不适用于在Windows中构建C代码。

我想忽略我的代码现在所做的事情并提出以下问题。 对于那些熟悉这个C库“getopt.h”的人:如果它依赖于POSIX风格的命令行参数,是否可以在Windows中构建和运行我的代码? 或者我是否必须重新编写适用于Windows的代码,以不同方式传递输入文件(并放弃“getopt.h”依赖关系)?

你是对的。 getopt()是POSIX,而不是Windows,您通常必须重新编写所有命令行参数解析代码。

幸运的是,有一个项目,Xgetopt,适用于Windows / MFC类。

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

如果你可以在你的项目中使用它,它应该为你节省大量编码,并防止你不得不重做所有的解析。

此外,它还带有一个很好的支持GUI的演示应用程序,您应该会发现它很有帮助。

祝好运!

getopt()实际上是一个非常简单的函数。 我为它做了一个github要点 , 这里的代码也在下面

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

我在windows下编译了getopt代码。

我这样做是因为我想在Windows(命令行)应用程序中使用其命令行解析function。

我用VC2010成功做到了这VC2010

据我所知,我没有遇到任何重大问题。

getopt.c getoptl.c

使用MinGW运行时代码(由Todd C. Miller编写)是有可能的:

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

我用这些文件和CMake脚本创建了一个小型库(可以生成一个VS项目):

https://github.com/alex85k/wingetopt

您可以尝试将glib-2.0作为替代方案。 只需要一个选项解析器就会有点大。 好的一面是可以使用所有其他精彩玩具。

说实话,我还没有尝试过这个(我主要坚持使用Linux),所以YMMV。

让glib在windows中工作: HowTo

哦,您可以探索使用mingw构建环境,以及IDE的visual studio。

Glib for Win32: HowTo

Anywho,希望这会有所帮助。

如果你只是想在没有其他依赖的情况下在visual c ++中使用getopt,我已经从最新的gnu libc 2.12移植了getopt.c,具有所有新function。唯一的区别是你必须使用TCHAR而不是char,但这是非常的在Windows中很常见。

只需下载源代码,制作,复制libgetopt.lib和getopt.h getopt_int.h到您的项目中。

您也可以使用根目录中的CMakeList.txt来创建它。

从github下载源代码

getopt.h存在于git中,我已下载它并且它适用于我:

https://gist.github.com/ashelly/7776712

根据我对getopt.h记忆,它所做的就是为你的main函数提供一个方便的解析器来处理argv:

 int main(int argc, char * argv[]) { } 

Windows控制台程序仍然有一个main方法,因此您可以简单地遍历argv数组并自己解析参数。 例如

 for ( int i = 1; i < argc; i++ ) { if (!strcmp(argv[i], "-f")) filename = argv[++i]; }