Hello World giving no output

Discussion in 'Mac Programming' started by mlagrone, Apr 11, 2007.

  1. macrumors newbie

    Joined:
    Apr 11, 2007
    #1
    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
     
  2. macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #2
    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! $ 
    
     
  3. macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #3
    Also I would recommend changing the line

    cout << "Hello World!";

    to

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

    Just a sugestion.
     
  4. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #4
    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
     
  5. thread starter macrumors newbie

    Joined:
    Apr 11, 2007
    #5
    Thank you

    Thanks for your help. Everything is working fine now
     

Share This Page