windows下的getopt/getoptlong函数
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.c和getopt1.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函数的更多相关文章
- windows下的getopt/getoptlong函数(拷贝GNU C的库函数)
http://www.cnblogs.com/oloroso/p/4856104.html
- [笔记]linux下和windows下的 创建线程函数
linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...
- 一个简单的Windows下的socket程序
服务器端代码server.cpp: #include <stdio.h> #include <WinSock2.h> #pragma comment(lib,"ws2 ...
- 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...
- windows下实现微秒级的延时
windowsintegeriostream汇编嵌入式任务 最近正在做一个嵌入式系统,是基于windows ce的,外接硬件的时序要微秒级的延时.1.微秒级的延时肯定不能基于消息(SetTimer函数 ...
- 使用cygwin移植Linux的项目到Windows下之总结(转)
使用cygwin移植Linux的项目到Windows下之总结(转) 原文 http://my.oschina.net/michaelyuanyuan/blog/68615?p=1 一.why ...
- c/c++在windows下获取时间和计算时间差的几种方法总结 【转】
http://blog.csdn.net/coder_xia/article/details/6566708 一.标准C和C++都可用 1.获取时间用time_t time( time_t * tim ...
- c和c++在windows下获取时间和计算时间差的方法总结
c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...
- Linux c 下使用getopt()函数
命令行参数解析函数 —— getopt() getopt()函数声明如下: #include <unistd.h> int getopt(int argc, char * const ar ...
随机推荐
- 自然语言20_The corpora with NLTK
QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed= ...
- Runner站立会议01
开会时间:9.10-9.30 地点:二教 今天做了什么:学了文本输入,button按钮,界面转换(意图) 遇到什么困难:界面转换时,出现问题,没有正常跳转 明天打所作什么:解决今天遇到的问题,学了第二 ...
- Ubuntu PostgreSQL安装和配置
一.安装 1.安装 使用如下命令,会自动安装最新版,这里为9.5 sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres&qu ...
- 入门:PHP:hello world!
<?php echo 'hello'."\n"." world!"."good night!";//2016.09.18 22:57? ...
- js变量在属性里的写法 常用mark 多个DL遍历添加一个父级DIV
标记用 js变量比如url链接一般都是a里面的href属性值 在js里单引号链接 以后再忘记就能有地方找了 例子: /* 添加1200 居中div 包裹 获取元素集合 上层元素100% * @ele ...
- Linux的io机制
Linux的io机制 Buffered-IO 和Direct-IO Linux磁盘I/O分为Buffered IO和Direct IO,这两者有何区别呢? 对于Buffered IO: 当应用程序尝试 ...
- MVC学习笔记一
主要是为了复习昨天所学习到的MVC的基础内容,因为昨天还在申请博客,所以今天补上. 目前主要学习资料是<ASP.NET MVC4 Web 编程> 首先先来一个MVC请求的路径的流程说明. ...
- css居中总结
水平居中 1. inline和inline-*元素水平居中:text-align:center 2. block元素水平居中: block定宽:margin-left: auto; margin-ri ...
- PHP使用数据库的并发问题(转)
在并行系统中并发问题永远不可忽视.尽管PHP语言原生没有提供多线程机制,那并不意味着所有的操作都是线程安全的.尤其是在操作诸如订单.支付等业务系统中,更需要注意操作数据库的并发问题. 接下来我通过一个 ...
- Linux服务器管理: 系统的进程管理pstree命令
pstree命令是查看进程树或者结构的命令 [root@localhost~]#pstree [选项] 需要注意的是不能将 -p和-u同时使用 如果同时使用前者生效后者无效但并不报错 选项: -p: ...