PDA

View Full Version : Hello World giving no output




mlagrone
Apr 11, 2007, 09:29 PM
I've tried running a simple Hello World Program on my new mac in both xcode and using text edit and g++. It says it executes successfully but there is no out put, it just runs then returns. Here is the code and response from the terminal.

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}

179-100:~/C++ Projects mlagrone$ g++ -g -O -c main.cpp
179-100:~/C++ Projects mlagrone$ g++ -g -O -c main.cpp
179-100:~/C++ Projects mlagrone$ g++ -g -O -c main.cpp
179-100:~/C++ Projects mlagrone$ g++ -g main.cpp
179-100:~/C++ Projects mlagrone$ g++ main.cpp
179-100:~/C++ Projects mlagrone$ g++ -c main.cpp
179-100:~/C++ Projects mlagrone$ g++ -g -frepo -O -c main.cpp
179-100:~/C++ Projects mlagrone$ g++ main.cpp
179-100:~/C++ Projects mlagrone$ g++ main.cpp

As you can see, no output, just back to command line. Can anyone help me?


Thx



WebMongol
Apr 11, 2007, 11:47 PM
To see output of your program you need to execute it:

$ g++ main.cpp && ./a.out
Hello World! $

Output of g++ is compiled binary with default name a.out.
If you want a different name - use -o <prgname> option:

$ g++ main.cpp -o xmain
$ ./xmain
Hello World! $

cruzrojas
Apr 12, 2007, 12:37 AM
Also I would recommend changing the line

cout << "Hello World!";

to

cout << "Hello World!" << endl;

Just a sugestion.

slooksterPSV
Apr 12, 2007, 10:35 PM
You can also make a simple script to automate all this for you like so:

g++ $1 && ./a.out

If you want to do this then follow these directions:
Go to a terminal
type in: pico name_of_your_command
example:
pico makeapp
next type:
g++ $1 && ./a.out
then press CTRL + O then press enter to save
then CTRL + X to exit pico editor
then type into the terminal prompt:
chmod +x ./name_of_your_command
example:
chmod +x ./makeapp
sudo mv name_of_your_command /usr/bin
example:
sudo mv makeapp /usr/bin

I recommend using the name makeapp cause I don't think anything uses that command.
Now type in: makeapp test.cpp or whatever you programming files are. If you need to link something in you'll have to either modify makeapp or make another command like makeapp2 or just do it as g++ -l linked_objects cpp_file

Now lets say you wanted to do the g++ thing but also specify a filename then do this:
g++ $1 -o $2 && ./$2
and then when you type in makeapp you give it to arguments:
makeapp test.cpp myapp

mlagrone
Apr 13, 2007, 11:54 AM
Thanks for your help. Everything is working fine now