如何在c++ /C中处理getopt平台依赖
How to handle getopt platform dependency in C++/C
下面是我使用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机器上工作呢?我不想使用boost
谢谢D. L. Kumar
如果,如您所说,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
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何用数字处理log(0)
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- 错误处理.将系统错误代码映射到泛型
- 从文本文件中读取时钟时间和事件时间并进行处理
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 用于矢量处理的多个线程
- 对字符串进行排序时,在c++中处理sort()
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 处理除以零会导致<csignal>意外行为
- 是否可以在c++中处理字符串流中的各个元素
- 如何在c++ /C中处理getopt平台依赖