【问题标题】:Executing std::cout doesn't print in running mode , but does in debugging mode执行 std::cout 不会在运行模式下打印,但会在调试模式下打印
【发布时间】:2025-12-08 03:50:01
【问题描述】:

这很奇怪,给定这段代码:

#include <iostream>
#include "DummySubject.h"
#include "Shop.h"

int main(int argc, char* argv[])
{
    std::cout << "1234567" << std::endl;
    // more code 
    return 0;
}

当我在运行模式下执行此代码时,它不会在控制台上打印任何内容,但是当我在调试模式下执行它时,cout 会在屏幕上打印1234567

知道问题可能是什么吗?

我什至使用了std::flush,但没有帮助。

编辑:

没有包含:

#include <iostream>
//#include "DummySubject.h"
//#include "Shop.h"

int main(int argc, char* argv[])
{
    std::cout << "1234567" << std::endl;

//  DummySubject mySubject;
//
//    // We have four shops wanting to keep updated price set by mySubject owner
//    Shop shop1("Shop 1");
//    Shop shop2("Shop 2");
//
//    mySubject.Attach(&shop1);
//    mySubject.Attach(&shop2);
//
//    //Now lets try changing the products price, this should update the shops automatically
//    mySubject.ChangePrice(23.0f);
//
//    //Now shop2 is not interested in new prices so they unsubscribe
//    mySubject.Detach(&shop2);
//
//    //Now lets try changing the products price again
//    mySubject.ChangePrice(26.0f);


    return 0;
}

还是不行。

【问题讨论】:

  • 如果您在iostream 之后删除这两个包含,那么它会起作用吗?
  • 检查“DummySubject.h”或“Shop.h”是否做了一些奇怪的事情。
  • @TommyA:没有帮助,请查看修改后的代码,谢谢。
  • @101010:没有帮助,请参阅编辑,谢谢。
  • @ron *.com/questions/3443254/… 可能是相关的。

标签: c++ eclipse c++11 mingw


【解决方案1】:

这是答案:

取自here

这适用于我在 Windows 7 上使用 64 位安装 Eclipse 明威:

右键单击您的项目。选择“属性”。

选择新建左侧的“运行/调试设置”属性 窗口。

在右侧窗口中,单击您的可执行文件以突出显示(即 - Test.exe),然后点击“编辑”。

在环境选项卡中,点击“新建”

名称:PATH 值:MinGW bin 目录的路径。 (对我来说这是: C:\devcore\MinGW\bin)

在所有窗口上单击“确定”关闭。

再次尝试运行,它应该将输出打印到屏幕上。

【讨论】: