Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ debug mode in eclipse causes program to not wait for cin

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;
    }
like image 572
Sentinel Avatar asked May 31 '17 11:05

Sentinel


1 Answers

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

enter image description here


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: example

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. debugger tab

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: 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.

like image 139
Jonah Graham Avatar answered Nov 14 '22 12:11

Jonah Graham