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 ...
随机推荐
- Nginx环境下http和https可同时访问方法
给nginx配置SSL证书之后,https可以正常访问,http访问显示400错误,nginx的配置如下: server { listen 80 default backlog=2048; liste ...
- PyCharm 教程(五)断点 调试
PyCharm 教程(五)断点 调试 PyCharm 作为IDE,断点调试是必须有的功能.否则,我们还真不如用纯编辑器写的快. [运行]和[调试]前的设置,详见前面的文章,helloword. 1,设 ...
- ecshop 活动-》红包
按商品发放:可以给指定某个商品发红包(购买付款,卖家发货后,会自动给买家发送红包:不是买家在付款的时候就自动可以减少红包金额) 按订单金额发放:订单满xx后(卖家发货后,会自动给买家发放红包)
- css隔行换色
.tab1 tr:nth-of-type(odd){ background:#eeeeee;} .tab1 tr:nth-of-type(even){ background:#f8f8f8;} .ta ...
- oracle安装常见问题
版本信息:CentOS6.5 + oracle11G 1.监视器颜色错误: [oracle@bogon database]$ 正在启动 Oracle Universal Installer... 检查 ...
- ReSharper 配置及用法(转)
1:安装后,Resharper会用他自己的英文智能提示,替换掉 vs2010的智能提示,所以我们要换回到vs2010的智能提示 2:快捷键.是使用vs2010的快捷键还是使用 Resharper的快捷 ...
- memcache相同主域名下的session共享
本配置适合具有相同主域名的多台服务器进行session共享. 例如:www.lee.com , bbs.lee.com(多个子域名). 配置session保存在memcache: ini_set(&q ...
- python mysql
mysql Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get install python-mysqldb Wi ...
- css3实现渐变的iPhone滑动解锁效果
先贴代码 <!DOCTYPE html> <html> <head> <style> p{ width:50%; margin:0 auto; line ...
- linq/EF/lambda 比较字符串日期时间大小
在使用EF时,想要比较字符串类型的日期时,参考以下: SQL语句: 1 2 3 4 1)select * from TableName where StartTime > '2015-04-08 ...