我试图在Windows下编译一组九个*.c文件(及相应的九个*.h文件)。
原始代码使用标准的GNU-Linux/C库“getopt.h”在Linux中接收命令行参数。而该库无法用于在Windows中构建C代码。
我想忽略现有代码的功能,问以下问题:熟悉C库“getopt.h”的人是否可以在基于POSIX风格的命令行参数的情况下对我的代码进行构建和运行?还是我必须重写代码以适用于Windows,并以不同的方式传递输入文件(放弃"getopt.h"依赖)?
我试图在Windows下编译一组九个*.c文件(及相应的九个*.h文件)。
原始代码使用标准的GNU-Linux/C库“getopt.h”在Linux中接收命令行参数。而该库无法用于在Windows中构建C代码。
我想忽略现有代码的功能,问以下问题:熟悉C库“getopt.h”的人是否可以在基于POSIX风格的命令行参数的情况下对我的代码进行构建和运行?还是我必须重写代码以适用于Windows,并以不同的方式传递输入文件(放弃"getopt.h"依赖)?
getopt()
函数实际上非常简单。我为它制作了github gist,下面是源代码(来源)
#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 */
}
extern char *optarg;
和 extern int optind, opterr, optopt;
。 - cbr你是正确的。 getopt()
是 POSIX 而不是 Windows,你一般需要重新编写所有解析命令行参数的代码。
幸运的是,有一个名为 Xgetopt 的项目专门为 Windows/MFC 类提供支持。
http://www.codeproject.com/Articles/1940/XGetopt-A-Unix-compatible-getopt-for-MFC-and-Win32
如果你能在你的项目中使其工作,它应该能够节省你相当多的编码时间,并防止你不得不重新构建所有解析代码。
此外,它还附带了一个很好的 GUI 演示应用程序,你会觉得它非常有用。
祝你好运!
getopt
没有出现任何问题 :-) 它可能还年轻或者已经很成熟了... 你可以尝试从我的回答中的链接中拉取 getopt
的源代码并使用它们。 - alk有可能使用来自MinGW运行时(由Todd C. Miller创建)的代码:
http://sourceforge.net/apps/trac/mingw-w64/browser/trunk/mingw-w64-crt/misc
我已经使用这些文件和CMake脚本创建了一个小型库(可以生成VS项目):
getopt.in.h
吗? - Victor Sergienkogetopt
的源代码需要一些来自getopt.in.h
的代码。无论如何,我的回答并不是要让人们误解,即在没有任何(小)修改的情况下编译链接文件是可能的。只是将它们“原样”提供给VC10是行不通的。 - alk从我对 getopt.h
的记忆中,它所做的就是提供一个方便的解析器来处理你的主函数中的 argv 参数:
int main(int argc, char * argv[])
{
}
Windows控制台程序仍然有一个主方法,因此您可以简单地循环遍历argv数组并自行解析参数。例如:
for ( int i = 1; i < argc; i++ )
{
if (!strcmp(argv[i], "-f"))
filename = argv[++i];
}
getopt
API 对我来说看起来非常稳定。@theJollySin - alkgetopt.h存在于Git中,我已经下载并且它对我有效:
getopt.h
描述了getopt.c
中提供的 API。从某个地方获取它,编译并将结果链接到您的应用程序中,就完成了。 - alk