1

以下是我使用 getopt 的小代码片段,它在我的 linux 机器上运行良好,但在 solaris 机器上运行良好。这是我在互联网其他地方找到的标准代码片段。

while ((c = getopt(argc, argv, "ab:")) != -1) {
  cout << "I am solaris, I dont come here \n";
  switch(c) {
    case 'a':
    case 'b':
  }
}

现在我的linux机器上没有问题。它做得很好。但是在我的 solaris 机器上,它甚至没有进入 while 循环,所以它不会为我解析任何东西。我在我的 solaris 机器上检查了“man getopt”(因为我认为使用了 shell 中的 getopt),它只是说下一个主要版本将不支持 getopt。

那么我怎样才能让它在我的 solaris 机器上工作。我不想使用提升。

谢谢 DL 库马尔

4

1 回答 1

1

如果,如您所说,Solaris 不会在下一个主要版本中支持 getopt,那么当不在 GNU/Linux 上编译时,您需要使用自己的 IF/DEF 宏。沿着这条线的东西:

#IFDEF _SOLARIS_
for (int index=0; index < argv; ++index)
{
  c = argc[index];  
  switch(c) {
    case 'a':
     // do your code
    case 'b':
     index++;
     if (index < argc)
       PARAMATER = arg[index]; // plucks the parameter
     else
       HANDLE MISSING ERROR
     // do your code
  }
}
#ELSE
while ((c = getopt(argc, argv, "ab:")) != -1) {
  cout << "I am solaris, I dont come here \n";
  switch(c) {
    case 'a':
    case 'b':
  }
}
#ENDIF
于 2011-10-17T19:36:14.023 回答