简体   繁体   English

Eclipse CDT中的调试问题

[英]Problem with Debugging in Eclipse CDT

I'm trying to debug the simple Hello World program that ships with Eclipse CDT. 我正在尝试调试Eclipse CDT附带的简单Hello World程序。 Running the program normally works fine, but when its run through debug mode, puts() doesn't print anything to the console. 运行该程序通常可以正常运行,但是在调试模式下运行时, puts()不会在控制台上显示任何内容。

I tried running the same program with gdb directly and it works fine, printing "!!!Hello World!!!" 我尝试直接用gdb运行相同的程序,但效果很好,打印出"!!!Hello World!!!" as expected. 如预期的那样。

Why doesn't puts() print anything when running in debug mode through Eclipse? 通过Eclipse在调试模式下运行时,为什么puts()打印任何内容?

I'm on Windows with MinGW installed, running gcc 4.5.0, gdb 7.2, and CDT 7.0.1 我在装有MinGW的Windows上,运行gcc 4.5.0,gdb 7.2和CDT 7.0.1

Thanks to Swiss pointed out the right direction. 感谢瑞士指出了正确的方向。
Adding fflush(stdout) after every printf , puts sentence is not suitable for a large project when debugging (When releasing , your'd better using fflush() at a appropriate time). 调试时,在每个printf之后添加fflush(stdout)puts语句不适合大型项目( 发布时 ,最好在适当的时候使用fflush() )。
Then, we can use Preprocessor directives #ifdef && setbuf() . 然后,我们可以使用预处理程序指令#ifdef && setbuf()
In Eclipse , your C project -> properties -> C/C++ Build -> Settings : Confgiguration = "Debug [ Active ]" -> Tool Settings -> GCC C Compiler -> Symbols -> Add "_DEBUG", 在Eclipse中,您的C项目->属性-> C / C ++构建->设置:配置=“ Debug [活动]”->工具设置-> GCC C编译器->符号->添加“ _DEBUG”,

Then in your main(), using: 然后在您的main()中,使用:

#ifdef _DEBUG
setbuf(stdout,NULL); // this disables buffering for stdout.
#endif

Have you tried to append \\n in the puts statement? 您是否尝试在puts语句中附加\\ n? Ie puts("Hello World!\\n"); 即放(“ Hello World!\\ n”); Some times terminal needs \\n to flush the stream. 有时终端需要\\ n刷新流。

I figured out why this happens. 我弄清楚了为什么会这样。 When running GDB through a terminal, STDOUT is line buffered, which means that it will be flushed every time a newline is read, resulting in the behavior I was expecting. 通过终端运行GDB时,STDOUT是行缓冲的,这意味着每次读取换行符时都会刷新它,从而导致了我所期望的行为。

HOWEVER! 然而! Eclipse CDT runs GDB with STDOUT in block buffered mode, which means it will only flush STDOUT when it reaches a certain size, or if it is manually flushed. Eclipse CDT在块缓冲模式下运行带有STDOUT的GDB,这意味着它仅在达到一定大小或手动刷新时才刷新STDOUT。 Since the string "!!!Hello World!!!" 由于字符串“ !!! Hello World !!!” is so short, the buffer for STDOUT will never be flushed unless a call to fflush() is made. 太短了,除非调用fflush()否则STDOUT的缓冲区将永远不会被刷新。

I managed to fix this problem by adding fflush(stdout); 我设法通过添加fflush(stdout);来解决此问题fflush(stdout); after the call to puts() . puts()调用之后。

Here's the resulting code: 这是结果代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    puts("Hello World!"); /* prints !!!Hello World!!! */
    fflush(stdout);
    return EXIT_SUCCESS;
}

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

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