Eclipse CDT 调用printf/cout 控制台(console)无输出

症状描述:

用Eclipse调试程序,执行printf和cout函数,但是console无内容显示。

 

原因分析:

Eclipse输出的内容是保存在buffer中,因此要显示相关内容,就必须刷buffer缓冲区。

 

解决方案:

1.在main函数开始时调用函数 setbuf(stdout,NULL);

2.在每个printf函数后调用函数 fflush(stdout);

 

int main(void) {
    setbuf(stdout, NULL);
    char* c="!!!Hello C!!!";
    printf(c); /* prints !!!Hello World!!! */
    //fflush(stdout);
    return EXIT_SUCCESS;
}
字符串c结尾没加\n,调试时报以下错误:
!!!Hello C!!!*stopped,reason="end-stepping-range",frame={addr="0x0040140f",func="main",args=[],file="..\src\HelloC.c",fullname="F:\\316\322\265\304\316\304\265\265\Workspaces\HelloC\Debug/..\src\HelloC.c",line="24"},thread-id="1",stopped-threads="all"

加上\n就好了。
int main(void) {
    setbuf(stdout, NULL);
    char* c="!!!Hello C!!!\n";
    printf(c); /* prints !!!Hello World!!! */
    //fflush(stdout);
    return EXIT_SUCCESS;
}


这是另一篇博客:

症状描述:使用Eclipse运行或者调试C/C++应用程序时,printf和cout调用无法实时输出到控制台

原因分析:当程序在Eclipse中运行时,默认把输出输出关联到了Eclipse的console,而CDT的console是带输出缓存的,所以会出现printf/cout调用时控制台(console)无输出情况,事实上是这个输出没有被实时输出到console上。

解决方案:

1, 代码全局设置:在main函数入口处设置stdout和stderr的缓存区为NULL,即调用 setbuf(stdout, NULL) 和 setbuf(stderr, NULL)

2, 代码局部设置:在每个printf/cout类似输出的地方调用fflush(stdout)或者fflush(stderr)

3,CDT全局设置:找半天没找到设置的地方:(,如果你知道的话,麻烦告诉我啊:)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值