C ++ debug mode in eclipse makes the program not wait for cin

The following code works fine when it runs, but there is a problem when it runs in debug mode with Eclipse, it doesn't wait for input and instead just keeps moving 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;
    }

      

+3


source to share


1 answer


Update

As of CDT 9.4 (Eclipse Oxygen.2) there is now a checkbox in the launch config 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 that there are two readers on the same stdin pipe. When you do cin

Eclipse CDT also tries to read stdin for GDB-MI communication.

Fortunately, there is a workaround, you can create a separate console for the program to run in GDB. This means no pen sharing.



To do this, create a file .gdbinit

in the root of the project with this content:

set new-console on

      

and debug your console app in Eclipse down to your heart content: example

Additional Information

You can install the gdbinit file for the Debug Configuration in the Debugger tab. Set the GDB command file to the name of the file you created. debugger tab

You can set the default GDB command file for newly generated Debug configurations by editing the preference in the C / C ++ -> Debug -> GDB page: GDB page

Eclipse CDT does not use .gdbinit in your home directory. This is on purpose because .gdbinit, which is generally configured for CLI debugging and can easily interfere with the MI intuition Eclipse needs to communicate with GDB correctly.

+2


source







All Articles