Click to See Complete Forum and Search --> : Question about gcc...I can't print to stdout


marc
08-26-2001, 01:26 AM
I'm new at all this...I just installed Mandrake today. I've used linux and gcc before, just never been responsible for setting it up....

anyway, I wrote a simple "hello world" program in C++ to test gcc. It compiles fine (as a software engineer, I'm sure I didn't make any mistakes in the one line of code...) but when I run the executable, it doesn't do anything. However, if I redirect output to a file, it does print there just fine.

I also tried the same thing in perl, and I used g++ and gcc (c++ and straight c) and got the same results for all three...

The only thing that worked was a shell script that used 'echo'. Does anyone know if I have some setting wrong? The installation was really straight forward. Since the shell script worked, I assume STDOUT is still the screen....

anyone ever heard of this?

error27
08-26-2001, 02:36 AM
i've never heard of anything like that and I can't think of why echo would be any different from your program.

are you doing this from an actual terminal or from a terminal emulator like Eterm or whatever?

Also try running it as root? That would at least answer your permision theory maybe...

marc
08-26-2001, 02:54 AM
I'm just using the Gnome terminal...

I tried it as root, and got the same thing.

Malakin
08-26-2001, 04:12 AM
If you're net getting any errors it must be spitting it out somewhere. Hit ctrl-alt-f2, login and try running it from there instead and see what happens. (I'm sure you know this but ctrl-alt-f7 to get back to the gui)

I realize you said you're a software engineer and you therefore know about 100 times more then me but just to make this really clear. I'm using mandrake 8 and using the following procedure it works for me, I'm not using the gnome terminal though (konsole in kde).

sample program:

#include <iostream.h>
main()
{
cout << "hello";
}

compile:

g++ hello.cpp -o hello

execute:

./hello

[ 26 August 2001: Message edited by: idealego ]

bdg1983
08-26-2001, 06:19 AM
If the directory the hello binary resides in is not in your $PATH, are you preceding the filename with ./ ?

./hello

Strike
08-26-2001, 11:44 AM
Even though you are a software engineer and this is the simplest program ever, could you put the code here and the line you used to compile it?

marc
08-26-2001, 01:36 PM
Ok, I got it to print...here's what I was doing.

#include <iostream.h>

void main (void)
{
cout << "hello";
}

Then to compile:
g++ hello.cpp -o hello

Then to run:
./hello

That was producing nothing on the screen (unless I redirected the output, then it would correctly print to the redirected file).
When I added a newline to the code:
cout << "hello\n";

it worked....

It's like, the program would print, and then the terminal would print the prompt line over the output.....weird. I've seen on other various *nix systems where, if you didn't put a newline at the end, the program would print before the prompt, but on the same line, so it would look something like:
hello[myprompt]>

but this was just showing nothing...

klamath
08-26-2001, 01:36 PM
As a software engineer, you *did* terminate the "Hello, World" with a newline ("\n"), right? :)

marc
08-26-2001, 02:04 PM
Originally posted by klamath:
<STRONG>As a software engineer, you *did* terminate the "Hello, World" with a newline ("\n"), right? :)</STRONG>

Actually, I didn't....

I know it should be second nature to me...but I was just "toying" with this new thing. I've worked on lots of different systems, Linux (I don't know what distro), Unix (various) and of course windows. I have NEVER seen this before just because there is no newline character. Usually, as I said in my previous post, it just prints the line before the prompt on the next line...depending on the system.

Strike
08-26-2001, 02:09 PM
Yeah, it's because cout is buffered I/O and the stream isn't flushed until you hit a newline or do an explicit fflush. I thought that the compiler automatically did that at the end of all programs though ... weird.

marc
08-26-2001, 02:20 PM
Originally posted by Strike:
<STRONG>Yeah, it's because cout is buffered I/O and the stream isn't flushed until you hit a newline or do an explicit fflush. I thought that the compiler automatically did that at the end of all programs though ... weird.</STRONG>

That's not true....the stream is flushed when the program exits. Newline certainly doesn't flush the buffer.

Strike
08-26-2001, 06:40 PM
Originally posted by marc:
<STRONG>That's not true....the stream is flushed when the program exits. Newline certainly doesn't flush the buffer.</STRONG>
If that's so, then putting a newline wouldn't have been the solution to his problem. Also, someone on this mailing list seems to agree with me ( http://lists.linux-india.org/lists/linux-india-help/199911/msg00281.html).

marc
08-26-2001, 08:02 PM
Apparently, the program was still printing to STDOUT. I know this because when I redirected (e.g. ./hello &gt; output_file) the output file would successfully get the result.

It appears as if the console would just overwrite the output when there was not a newline...

The only thing I changd was the newline.

*checking URL now*

Just for kicks, I added an fflush(stdout) line....it still won't print without the ending newline. Now my code looks like:

(in main)
cout &lt;&lt; "hello";
fflush(stdout);

That still does not show on the screen. So, there is something about having that newline there....a newline does not flush the buffer (that would kind of defeat the purpose of having an I/O buffer), anyway, without the buffer, and adding the fflush, the buffer would be flushed, so it has nothing to do with the I/O buffer.

For some reason, at least in this one particular terminal/compiler (this doesn't happen with Visual Studio under Windows, CC under AIX for two example), the newline helps the terminal show the text...

[ 26 August 2001: Message edited by: marc ]

klamath
08-26-2001, 09:30 PM
What $SHELL are you using? On my system, using zsh will overwrite the output, but using bash will print the output followed immediately by the prompt. i.e.


[nconway:/home/nconway]% echo $SHELL
/usr/bin/zsh
[nconway:/home/nconway]% ./a.out
[nconway:/home/nconway]% sh
sh-2.05$ ./a.out
Hello, Worldsh-2.05$


(a.out is a compiled program that prints "Hello, World", without a newline)

weird...

marc
08-26-2001, 10:40 PM
bash