windows下的getopt/getoptlong函数

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.cgetopt1.c文件中都有,直接拿过来用了。

这里测试的时候没有区分是glibc-2.2.5还是glibc-2.15版本的getopt/getopt_long,因为两个测试的结果是一样的。

getopt()函数的测试

测试代码

 #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();
}

测试结果

windows下的getopt/getoptlong函数的更多相关文章

  1. windows下的getopt/getoptlong函数(拷贝GNU C的库函数)

    http://www.cnblogs.com/oloroso/p/4856104.html

  2. [笔记]linux下和windows下的 创建线程函数

    linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...

  3. 一个简单的Windows下的socket程序

    服务器端代码server.cpp: #include <stdio.h> #include <WinSock2.h> #pragma comment(lib,"ws2 ...

  4. 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结

    一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...

  5. windows下实现微秒级的延时

    windowsintegeriostream汇编嵌入式任务 最近正在做一个嵌入式系统,是基于windows ce的,外接硬件的时序要微秒级的延时.1.微秒级的延时肯定不能基于消息(SetTimer函数 ...

  6. 使用cygwin移植Linux的项目到Windows下之总结(转)

    使用cygwin移植Linux的项目到Windows下之总结(转) 原文 http://my.oschina.net/michaelyuanyuan/blog/68615?p=1   一.why   ...

  7. c/c++在windows下获取时间和计算时间差的几种方法总结 【转】

    http://blog.csdn.net/coder_xia/article/details/6566708 一.标准C和C++都可用 1.获取时间用time_t time( time_t * tim ...

  8. c和c++在windows下获取时间和计算时间差的方法总结

    c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...

  9. Linux c 下使用getopt()函数

    命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...

随机推荐

  1. JavaScript学习笔记——DOM_document对象

    javascript-document对象详解 DOM document(html xml) object modledocument对象(DOM核心对象) 作用: 1.内容 innerHTML 2. ...

  2. MinGW: TOO MANY SECTIONS issue

    http://sourceforge.net/p/mingw-w64/mailman/mingw-w64-public/thread/509A38C6.6070807@doxos.eu/ MingGW ...

  3. Javascript的匿名函数与自执行

    1.匿名函数 函数是JavaScript中最灵活的一种对象,这里只是讲解其匿名函数的用途.匿名函数:就是没有函数名的函数. 1.1 函数的定义,首先简单介绍一下函数的定义,大致可分为三种方式 第一种: ...

  4. Object.prototype.toString.call() 区分对象类型

    判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...

  5. HTML5基本布局

    HTML4布局 HTML5布局 基本的HTML5文档的模式为: <!DOCTYPE html> <html lang = "en"> <head> ...

  6. Unity 播放 视频

    Unity3D 播放视频 http://www.cnblogs.com/fortomorrow/archive/2012/11/01/unity07.html Unity3D研究院之两种方式播放游戏视 ...

  7. DWZ框架中ajax提交文件表单的处理(关闭当前dialog + 刷新父级navTab)

    先重点关注两个js文件:dwz.ajax.js和dwz.core.js 流程: 1.回调iframeCallback <form xx enctype="multipart/form- ...

  8. shell编程中的select用法

    select 语句 select表达式是bash的一种扩展应用,擅长于交互式场合.用户可以从一组不同的值中进行选择: select var in ... ; do break; done .... n ...

  9. MySQL的Order By Rand()的效率问题

    MySQL很多时候需要获取随机数据,举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是: 但是,后来我查了一下MYSQL的官方手册,里面针对RAND()的提示大概意思就是,在OR ...

  10. linux系统安装yum环境

    http://blog.sina.com.cn/s/blog_63d8dad80101cn2s.html 1.卸载rhel的默认安装的yum包 查看yum包 rpm -qa|grep yum 卸载之 ...