eclipse中的c ++调试模式导致程序不等待cin [英] c++ debug mode in eclipse causes program to not wait for cin

查看:148
本文介绍了eclipse中的c ++调试模式导致程序不等待cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在运行时可以正常工作,但是在使用Eclipse以调试模式运行时会出现问题,它不等待输入,而是继续运行,并且一些看似随机的值被打印到控制台。

  int main(){

int N, Q,maxSize;
cout<<输入数字<< endl;
int测试;
cin>>测试;
cout<<测试<< endl;
}


解决方案

更新



在CDT 9.4(Eclipse Oxygen.2)中,现在在启动配置中有一个复选框,可以一键式完成。参见






原始答案



这里的问题是在同一stdin频道上有两个读者。当您执行 cin 时,Eclipse CDT也在尝试读取stdin进行GDB-MI通信。



幸运的是,有一种解决方法,您可以让GDB为正在运行的程序创建一个单独的控制台。



为此,请在项目的根目录中创建一个 .gdbinit 文件。包含以下内容:

 


并在Eclipse中调试控制台应用以适应您的内心需求:



更多信息



您可以在 Debugger 标签中设置gdbinit文件用于调试配置。将 GDB命令文件设置为您创建的文件的名称。



您可以设置默认的 GDB命令文件用于新创建的调试配置通过在 C / C ++ -> Debug -> GDB 页面中编辑首选项:



Eclipse CDT在您的主目录中不使用.gdbinit。这是有意的,因为通常在其中设置.gdbinit以便进行CLI调试,并且可以轻易干扰Eclipse正确地与GDB通信所需的MI接口。


The following code works fine when it's run but there's an issue when it's run in debug mode using Eclipse, it does not wait for input and instead just keeps going and some seemingly random value is printed to the console. It also won't stop at a breakpoint.

int main() {

        int N, Q, maxSize;
        cout <<"Enter a number"<<endl;
        int test;
        cin >> test;
        cout << test <<endl;
    }

解决方案

Update

From CDT 9.4 (Eclipse Oxygen.2) there is now a checkbox in the launch configuration to do this with one-click. See https://wiki.eclipse.org/CDT/User/NewIn94#Debug


Original answer

The problem here is there are two readers on the same stdin channel. When you do cin Eclipse CDT is also trying to read stdin for the GDB-MI communication.

Fortunately there is a workaround, you can have GDB create a separate console for the program that is running. That means no sharing of the handles.

To do so create a .gdbinit file in the root of the project with these contents:

set new-console on

and debug your console app in Eclipse to your heart's content:

More Info

You can set the gdbinit file to use for your Debug Configuration in the Debugger tab. Set the GDB Command file to the name of the file you have created.

You can set the default GDB Command file to use for newly created Debug Configurations by editing the preference in C/C++ -> Debug -> GDB page:

Eclipse CDT does not use the .gdbinit in your home directory. This is on purpose because the .gdbinit that is there is normally set up for CLI debugging and can easily interfere with the MI inteface needed for Eclipse to communicate with GDB properly.

这篇关于eclipse中的c ++调试模式导致程序不等待cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆