Guillaume
Guillaume

Reputation: 495

Eclipse CDT using MinGW does not output in console

I have a Windows 7 64-bit PC and I am trying to install a free C++ IDE, so I chose to install Eclipse Helios with CDT.

For g++, make and gdb I installed msys and mingw according to this tutorial: http://wiki.wxwidgets.org/HowTo:_Install_MSYS_and_MinGW_for_use_with_Eclipse_CDT

The versions are:

So it should be compatible with my 64-bit CPU.

However when I try to run a very simple program with eclipse, I see nothing on the console. The source code is:

#include <iostream>

 using namespace std;

 int main()
 {
    int i;

    cout << "Enter an integer: " << endl;
    cin >> i;
    cout << endl << "i is " << i << endl;

    return 0;
 }

The build is fine and when I launch the .exe with command (windows console) the behavior is as expected. But with the Eclipse console I see nothing with run and with debug the output is just: "Enter an integer: ", then when I type in a number and hit enter it does nothing.

Does anyone know how to fix this please?

Thanks,

Guillaume

PS: I use the toolchain "Linux GCC", with "MinGW GCC" I have nothing at all in the console.

Upvotes: 38

Views: 50109

Answers (10)

ZiglioUK
ZiglioUK

Reputation: 2610

I've asked ChatGPT and it suggested me many approaches but this one works for me: "Use external console"

Debug Config

Upvotes: 0

zaursh
zaursh

Reputation: 49

Add PATH variable (PATH="your MinGW/bin directory path") into your C++ project by Run -> Run Configurations ->in Environment Tab

enter image description here

Upvotes: 1

JoeManiaci
JoeManiaci

Reputation: 475

Had this issue on 64-/32-bit eclipse Kepler CDT to work on a openCV/wxWidgets tool, Win7, using MinGW to build.

If anyone comes across this while having this issue and are working with openCV you will already know that there are many outdated openCV building/installing instructions all over the internet.

One I had was to go to Build Settings -> Linker -> Miscellaneous and inside of the Linker Flags text entry box, type in -Wl,--subsystem,windows -mwindows However, this disables cout from outputting to a command line terminal in windows.

Doing some more digging this looks intentional, apparently the -mwindows involves directing STDOUT away from a command line specifically to a GUI-like application.

Also, removing -mwindows and just leaving in -Wl,--subsystem,windows accomplishes the task of redirecting STDOUT anyway away from the command line all the same.

Now mind you, I haven't built up anything yet outside of a hello world program involving wxWidgets and openCV, so I am not at the point of doing a cout into a part of a GUI so I don't know if that functionality would now be broken or if it would print out to the GUI object, as well as a command line terminal.

Upvotes: 1

michael_s
michael_s

Reputation: 2575

I ran into the same problem, because of multiple gcc installations on one PC. But Greg's solution only worked partly for me.

In my case the flush was not done in the application explicitly. While C++ programs often use std::cout << ... << std::endl where the endl does a flush, my program used actual C-output such as the usual printf. The printf could be seen directly when starting the program in the cmd-window. However in eclipse console they were missing. Hence a

fflush(stdout);

after the printf did the thing for me. That could be an issue within the eclipse console implementation. I guess that's why fixing the Path did not work for some people here.

An alternative solution instead of setting the PATH within the "Run" settings is to start the whole eclipse using a batch file, which looks essentially like this:

set PATH=<mymingwlocation>\bin;%PATH%
start <myeclipselocation>\eclipse.exe

Then any run configuration would use the correct MingW location by default. That might also fix other problems that could arise from using the wrong gcc.

Upvotes: 3

Dhiral Pandya
Dhiral Pandya

Reputation: 10619

You need to set up linker I am using MinGW.

Follow below steps.

Goto Project > Properties > C/C++ Build > Settings > Tool Settings (Tab) > MinGW C++ Linker (Option) > Add Command (g++ -static-libgcc -static-libstdc++)   (default command is only g++)

Upvotes: 8

ossys
ossys

Reputation: 4217

This worked for me on 64-bit install of Eclipse on Windows 7 using MinGW:

Right-click on your project. Select "Properties".

Select the "Run/Debug Settings" Property on the left of the new window.

In the right window, click on your executable to highlight (ie - Test.exe) and click "Edit".

In the Environment tab, hit "New"

Name: PATH
Value: Path to your MinGW bin directory. (For me this was: C:\devcore\MinGW\bin)

Click "OK" on all windows to close down.

Try running again, it should print output to the screen.

Upvotes: 140

rmk
rmk

Reputation: 1

Or set the linker option -static. Works for me at least.

Upvotes: 0

Yifu
Yifu

Reputation: 265

purlogic's solution works. Instead to set that for every project, I found it can be set globally:

In Window -> Preferences-> C/C++ -> Build -> Environment Add a variable for your compiler. e.g, I added: MINGW, with value "C:\MinGW\bin"

Upvotes: 7

Roberto
Roberto

Reputation: 61

Have you tried to execute the eclipse.exe with administrator privileges ?? it worked for me !

Upvotes: 1

guest
guest

Reputation: 76

This console bug has been noticed in 64-bit versions of eclipse:

http://www.eclipse.org/forums/index.php?t=msg&th=197552&start=0&S=2a2b64e1f1404705c0214976bd477428

A workaround is to install the 32-bit eclipse

Upvotes: 6

Related Questions