使用getopt和gdb

bph*_*bph 5 c emacs gdb getopt

刚刚融入getopt我的main()功能

getoptoptarg为每个调用设置全局变量

通过加强main()gdb,后getopt()调用optarg总是NULL(例如(gdb) p optarg),但printf("%s\n", optarg)输出的CMD行参数如预期

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

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

Mor*_*pfh 2

可能相关:Bug 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
Run Code Online (Sandbox Code Playgroud)

在哪里:

(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);
Run Code Online (Sandbox Code Playgroud)