getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到。
这里就不介绍了。
windows下没有找到类似的函数,自己写一个又浪费时间,于是乎从glibc中找出来。
这里放出两个版本的下载地址
http://files.cnblogs.com/files/oloroso/getopt--from-glibc-2.15.tar.gz
http://files.cnblogs.com/files/oloroso/getopt-win-from-glibc-2.2.5.tar.gz
下载GLibC源码首先需要下载glibC的源代码文件,这个文件比较大,但是我们只需要其中的几个文件而已。
如果是后面给出的链接下载的glibc源码包,只需要两个文件。
如果是比较新版本的glibc,可能需要四个文件(getopt.h/getopt_int.h/getopt.c/getopt_init.c)
这个只需要图中所示的两个文件即可。
下载之后找到这两个文件解压出来即可。
下载地址 http://down1.chinaunix.net/distfiles/glibc-2.2.5.tar.bz2
这是glibc-2.2.5的压缩包中间提取的文件
下面是glibc-2.15中需要提取的文件
修改getopt.c gettext.h头文件不存在问题(glibc-2.15)首先需要修改的是没有“gettext.h”这个头文件的问题。
这里直接将其注释掉,然后修改后面的宏定义。
这个修改仅是glibc-2.15版本中,如果是glibc-2.2.5版本的,是没有这个问题的。
将下面的代码(大概在70行)
#ifdef _LIBC
# include <libintl.h>
#else
# include "gettext.h"
# define _(msgid) gettext (msgid)
#endif
修改为
#ifdef _LIBC
# include <libintl.h>
#else
//# include "gettext.h"
# define _(msgid) (msgid)
#endif
修改后
alloca的问题(无法解析的外部符号 _alloca)(glibc-2.15)关于alloca这个函数,从百度百科中摘抄下来一点。
alloca内存分配函数,与malloc,calloc,realloc类似。
但是注意一个重要的区别,_alloca是在栈(stack)上申请空间,用完马上就释放.
包含在头文件malloc.h中.在某些系统中会宏定义成_alloca使用。
在glibc-2.2.5版本是没有这个问题的,这个问题存在于glibc-2.15版本中。
编译生成的时候会报错
> getopt.c
>e:\getopt-win\getopt.c(): warning C4013: “alloca”未定义;假设外部返回 int
>e:\getopt-win\getopt.c(): warning C4047: “初始化”:“option_list *”与“int”的间接级别不同
>getopt.obj : error LNK2019: 无法解析的外部符号 _alloca,该符号在函数 __getopt_internal_r 中被引用
这里保存的原因是alloca这个函数没有定义,那么我们使用已经定义好的版本就是了。
修改成如下图所示即可
strings.h头文件不存在问题(glibc-2.2.5)这个修改和前面的gettext.h文件的修改类似,但是区别是这个问题在glibc-2.15中不存在。
这里的修改很简单,添加一个 HAVE_STRING_H 的宏定义即可。
修改前 修改后
添加getopt_long/getopt_long_only的定义
这两个函数在getopt.h文件中声明了,但是其定义在getopt1.c中,可以直接将getopt1.c文件也拿过来用。
因为这个文件中的内容不多,为了减少文件的数量,直接将其中有用的部分拷贝到getopt.c文件中是个不错的主意。
glibc-2.2.5版本中,要拷贝的内容如下
int
getopt_long (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, );
} /* Like getopt_long, but '-' as well as '--' can indicate a long option.
If an option that starts with '-' (not '--') doesn't match a long option,
but does match a short option, it is parsed as a short option
instead. */ int
getopt_long_only (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, );
}
如果是glibc-2.15版本的,除了这两个函数之外,还有两个可重入版本的可以添加进去
int测试一下
getopt_long(int argc, char *const *argv, const char *options,
const struct option *long_options, int *opt_index)
{
return _getopt_internal(argc, argv, options, long_options, opt_index, , );
} int
_getopt_long_r(int argc, char *const *argv, const char *options,
const struct option *long_options, int *opt_index,
struct _getopt_data *d)
{
return _getopt_internal_r(argc, argv, options, long_options, opt_index,
, d, );
} /* Like getopt_long, but '-' as well as '--' can indicate a long option.
If an option that starts with '-' (not '--') doesn't match a long option,
but does match a short option, it is parsed as a short option
instead. */ int
getopt_long_only (int argc, char *const *argv, const char *options,
const struct option *long_options, int *opt_index)
{
return _getopt_internal (argc, argv, options, long_options, opt_index, , );
} int
_getopt_long_only_r(int argc, char *const *argv, const char *options,
const struct option *long_options, int *opt_index,
struct _getopt_data *d)
{
return _getopt_internal_r(argc, argv, options, long_options, opt_index,
, d, );
}
经过上面的修改,可以进行一点简单的测试了。
测试用例不用自己写了,在getopt.c和getopt1.c文件中都有,直接拿过来用了。
这里测试的时候没有区分是glibc-2.2.5还是glibc-2.15版本的getopt/getopt_long,因为两个测试的结果是一样的。
测试代码
#include "getopt.h"
#include <stdlib.h>
#include <stdio.h> int
main(int argc, char **argv)
{
int c;
int digit_optind = ; while ()
{
int this_option_optind = optind ? optind : ; c = getopt(argc, argv, "abc:d:0123456789");
if (c == -)
break; switch (c)
{
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
if (digit_optind != && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value '%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
} if (optind < argc)
{
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
} exit();
}
测试结果
getopt_long的测试#include "getopt.h"
#include <stdlib.h>
#include <stdio.h>
int
main(argc, argv)
int argc; //这是早期的C语言函数参数的写法
char **argv; //现在不提倡这么写
{
int c;
int digit_optind = ; while ()
{
int this_option_optind = optind ? optind : ;
int option_index = ;
static struct option long_options[] =
{
{ "add", , , },
{ "append", , , },
{ "delete", , , },
{ "verbose", , , },
{ "create", , , },
{ "file", , , },
{ , , , }
}; c = getopt_long(argc, argv, "abc:d:0123456789",
long_options, &option_index);
if (c == -)
break; switch (c)
{
case :
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break; case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
if (digit_optind != && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value `%s'\n", optarg);
break;
case 'd':
printf("option d with value `%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
} if (optind < argc)
{
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
} exit();
}
测试结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)