使用getopt和gdb

刚刚将getopt合并到我的main()函数中

getopt为每个调用设置全局变量optarg

使用gdb步执行main() ,在getopt()调用optarg始终为NULL (例如(gdb) p optarg )而printf("%s\n", optarg)按预期输出cmd行arg

这是怎么回事? 为什么这两个不一样?

这是gdb的问题,以及它如何检查全局或其他事情?

可能相关: 错误13800 – gdb不会打印与getopt相关的值的正确值

另请注意:

 (gdb) n opt: 111, arg, 0x804a040 0x804a034 0x804a020 0x804a030 (gdb) printf "%p\n%p\n%p\n%p\n", &optarg, &opterr, &optind, &optopt 0x2ae760 0x2ab0f4 0x2ab0f8 0x2ab0f0 

哪里:

 (gdb) l 6 int main(int argc, char *argv[]) 7 { 8 int c; 9 while ((c = getopt(argc, argv, ":abf:o:")) != -1) { 10 printf("opt: %d, %s, \n" 11 "%p\n%p\n%p\n%p\n", 12 c, optarg, 13 &optarg, &opterr, &optind, &optopt);