简体   繁体   English

使用 GDB 在 Eclipse 中调试 C 程序时的奇怪行为

[英]Weird behaviour when debugging C programs in Eclipse using GDB

I was trying to debug a C program that requires user inputs, using Eclipse IDE and GDB debugger.我试图使用 Eclipse IDE 和 GDB 调试器调试需要用户输入的 C 程序。 Every time I entered a user input in the console, my input was displayed twice.每次我在控制台中输入用户输入时,我的输入都会显示两次。 In other words, my input was echoed with the same value.换句话说,我的输入得到了相同的值。 For example, if I debug the following C program:例如,如果我调试以下 C 程序:

#include <stdio.h>

int main(void) {
    int length, width, height;

    printf("Enter length: ");
    scanf("%d", &length);
    printf("Enter width: ");
    scanf("%d", &width);
    printf("Enter height: ");
    scanf("%d", &height);
    printf("Volume = %d\n", length*width*height);

    return 0;
}

, then the console will look something like this: ,那么控制台将如下所示:

Enter length: 2
2
Enter width: 3
3
Enter height: 4
4
Volume = 24

. .

As you can see, the values 2, 3 and 4 are displayed twice respectively.如您所见,值 2、3 和 4 分别显示了两次。 Running this program (instead of debugging) gives the expected result:运行这个程序(而不是调试)给出了预期的结果:

Enter length: 2
Enter width: 3
Enter height: 4
Volume = 24

In addition, I notice that the execution of printf() statements are delayed: although I have already clicked "step over", there is nothing shown in the console.另外,我注意到printf()语句的执行被延迟了:虽然我已经点击了“step over”,但控制台中没有任何显示。 To make this issue more obvious, let us change the code to:为了使这个问题更明显,让我们将代码更改为:

#include <stdio.h>

int main(void) {
    int length, width, height;

    printf("Enter length: ");
    printf("Enter length: "); // newly added line
    printf("Enter length: "); // newly added line
    scanf("%d", &length);
    printf("Enter width: ");
    scanf("%d", &width);
    printf("Enter height: ");
    scanf("%d", &height);
    printf("Volume = %d\n", length*width*height);

    return 0;
}

In this case, the string "Enter length: Enter length: Enter length: " will be displayed all at once, after I stepped over line 9: scanf("%d", &length);在这种情况下,字符串“输入长度:输入长度:输入长度:”将在我跨过第 9 行后立即显示: scanf("%d", &length); . . However, if I add a new line character '\\n' at the end every time I call printf() , then there will be no problem and the strings will be immediately printed to the console.但是,如果我每次调用printf()时在末尾添加一个新行字符 '\\n' ,那么就不会有问题并且字符串将立即打印到控制台。

I know that these two issues are trivial, but being OCD I really want to know what is going on here.我知道这两个问题是微不足道的,但作为强迫症我真的很想知道这里发生了什么。 I guess it has something to do with how Eclipse handles input and output?我猜这与 Eclipse 如何处理输入和输出有关? But then again I don't see these issues when debugging java programs in Eclipse.但话又说回来,在 Eclipse 中调试 Java 程序时,我没有看到这些问题。

PS.附注。 I am running on Mac OS X El Capitan, Eclipse (Mars) IDE for C/C++ Developers, and I installed GDB with Homebrew according to the instruction given here: http://ntraft.com/installing-gdb-on-os-x-mavericks/ .我在 Mac OS X El Capitan、C/C++ 开发人员的 Eclipse (Mars) IDE 上运行,我根据此处给出的说明安装了带有 Homebrew 的 GDB: http : //ntraft.com/installing-gdb-on-os- x-小牛/

Thanks in advance for answering.预先感谢您的回答。

The output is being buffered.输出正在缓冲。 A new line character flushes the buffer and displays the result on the screen.换行符刷新缓冲区并在屏幕上显示结果。 That's why you're not having the issule when you add \\n .这就是为什么添加\\n时没有问题的原因。

Alternatively, you can try adding fflush(stdout);或者,您可以尝试添加fflush(stdout); after your printf statements.在您的 printf 语句之后。

Also, I would also suggest you to use Xcode for Mac instead of Eclipse.另外,我还建议您使用 Xcode for Mac 而不是 Eclipse。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM