Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mlagrone

macrumors newbie
Original poster
Apr 11, 2007
2
0
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

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
Execute compiled binary

To see output of your program you need to execute it:
Code:
$ 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:
Code:
$ g++ main.cpp -o xmain
$ ./xmain
Hello World! $
 

cruzrojas

macrumors member
Mar 26, 2007
67
0
USA
Also I would recommend changing the line

cout << "Hello World!";

to

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

Just a sugestion.
 

slooksterPSV

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
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:
Code:
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:
Code:
chmod +x ./makeapp
sudo mv name_of_your_command /usr/bin
example:
Code:
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.