gpt4 book ai didi

c - getopt(3) 在 mac os 和 CentOS 上

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:30 31 4
gpt4 key购买 nike

我在 OSX 和 CentOS 上通过 gcc 编译相同的代码:

while (( opt = getopt (argc, argv, "hp:" )) != -1 )
{
fprintf (stderr,"+++++++++++ %d\n\n", opt );
switch ( opt )
{
case 'h':
fprintf(stderr, "Help Page \n %s", help_str);
return 1;

case 'p':
filename = optarg ;
fprintf(stderr,"================== %s\n",optarg);
break;

case '?':
printf("ZHZHZHZHZHUT\n");
return 3;

default:
break;
}
}

然后我尝试运行 ./a.out -p ./file.txt ya.ru./a.out ya.ru -p ./file.txt在 CentOS 6.5 上,两种变体都可以工作。

但在 MAC OS X(10.9) 变体 ./a.out ya.ru -p ./file.txt(依赖参数之前的自由参数)不起作用。-p/file.txt 用作简单参数(不是 getopt)。

完整代码是 here

谢谢。

最佳答案

documented GNU getopt() 的行为是仅当处于 POSIX 模式时才在第一个非选项参数处停止,这可以通过选项字符串开头的 + 进行设置或者通过设置 POSIXLY_CORRECT 环境变量。

在默认模式下,GNU getopt() “在扫描时置换 argv 的内容,因此最终所有非选项都在末尾。”

你遇到了这种行为上的差异——这是 GNU getopt() 和 BSD getopt() 行为之间的差异,因为 BSD getopt( ) 具有 POSIX 语义。

关于c - getopt(3) 在 mac os 和 CentOS 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23630424/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com