Starting problem, c++ with gcc/g++ compiler

Discussion in 'Mac Programming' started by tyskindian, May 10, 2009.

  1. tyskindian macrumors newbie

    Joined:
    May 10, 2009
    #1
    I started learning c++ many years ago in windows and now wants to recap that in OS X.

    This code:

    Code:
    #include <iostream>
    
    int main();
    {
    	cout << "Hello World!\n";
    	return 0;
    
    }
    Generates the following error:

    Code:
    hello.cpp:4: error: expected unqualified-id before ‘{’ token
    Whats wrong?

    And BTW. I tried first to include iostream.h but the compiler then warned that it is "decrepated". Should I no longer, for any cpp header, use ".h"?

    Thanks for all help!
     
  2. neonblue2 macrumors 6502a

    Joined:
    Aug 25, 2006
    Location:
    Port Pirie, South Australia
    #2
    A couple of problems.

    Code:
    #include <iostream>
    
    int main() //No semicolon needed here.
    {
    	[b]std::[/b]cout << "Hello World!\n";
    	return 0;
    }
    Some but not all. Headers like iostream and cstdlib don't have .h, but if you include C headers then you need the .h.
     
  3. tyskindian thread starter macrumors newbie

    Joined:
    May 10, 2009
    #3
    Thanks, but... just removing the semicolon makes no difference and if I before "cout" add "std::" I get the following from the compiler instead :confused::

    Code:
    Undefined symbols:
      "___gxx_personality_v0", referenced from:
          ___gxx_personality_v0$non_lazy_ptr in cceRTNkh.o
      "std::ios_base::Init::~Init()", referenced from:
          ___tcf_0 in cceRTNkh.o
      "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
          _main in cceRTNkh.o
      "std::ios_base::Init::Init()", referenced from:
          __static_initialization_and_destruction_0(int, int)in cceRTNkh.o
      "std::cout", referenced from:
          __ZSt4cout$non_lazy_ptr in cceRTNkh.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
     
  4. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #4
    Code:
    #include <iostream>
    
    int main () {
    [INDENT]std::cout << "Hello World!" << std::endl;
    return 0;[/INDENT]
    
    }
    
    The culprit is the '\n' whitespace char. C used this, but C++ uses 'std::endl'.
     
  5. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #5
    I just did the compile of the following:

    cppTest.C

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
    	std::cout << "Hello World!\n";
    	return 0;
    
    }
    
    
    using

    g++ cppTest.C -o cppTest

    it worked just fine...

    Did you install the Developer tools or did you use the built in compiler?
     
  6. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #6
    The "\n" is still valid in C++.

    You can even get rid of the std:: by doing the following:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	cout << "Hello World!\n";
    	return 0;
    
    }
    
    
     
  7. tyskindian thread starter macrumors newbie

    Joined:
    May 10, 2009
    #7
    This made no difference.

    This worked just fine - thanks! But what does "int argc, char *argv[]" mean? And yes, I did install the developer tools.

    Sweet! Thansk again, lets see how far I can make it on my own now...:)
     
  8. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #8
    Those are the arguments passed on the command line to the program.

    If you've ever done any terminal commands, ie.

    cp sourcefile destinationfile

    The sourcefile and destinationfile are both arguments to the program named cp.

    You will see that the argc counter is 2 and the argv * contains the strings for sourcefile and destinationfile.

    depending on the OS, the arguments would come out like this:

    argv[0] = "cp" -- this is the program name
    argv[1] = "sourcefile"
    argv[2] = "destinationfile"
     
  9. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #9
    Righty-o.

    Code:
    #include <iostream>
    
    int main () {
    [indent]std::cout << "Hello World!" << std::endl;
    return 0;[/indent]
    }
    Is working code...you don't need the extra command line input argument declarations for the main.

    So the code above using
    Code:
    g++ -o hello hello.cc
    doesn't work on your machine?
     
  10. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #10
    The real problem is that you were compiling your C++ program with a C compiler (that's where this ___gxx_personality_v0 stuff came from). GCC looks at the extension of your program to decide whether it's C (.c) or C++ (.cpp or .cc, or maybe .C). Of course, you can use "g++" instead of "gcc" to compile your program as well.

    Like sammich said, the argc/argv stuff has nothing to do with your problem and you can safely use int main() for your program.

    Good luck!
     
  11. jake.f macrumors 6502

    Joined:
    Sep 4, 2008
    Location:
    NSW, Australia
    #11
    Well im not sure about your compiler but when i made a hello world thing i used this code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
            cout << "Hello World!\n";
        
            return 0;
    }
     
  12. RaZaK macrumors regular

    RaZaK

    Joined:
    Jul 13, 2008
    #12
    Thanks

    I was getting funky output as well when trying to use gcc. at first, i thought it was a problem with using TextEdit.

    So, i switched to vi, got a different set of problems.

    After switching to g++ instead of gcc, everything is working fine now...

    only 23 more hours to go in the SAMS book. :D
     
  13. Dragonlance1561 macrumors member

    Dragonlance1561

    Joined:
    Aug 16, 2007
    #13
    I know someone who learned form that book, apparently it's not a bad one.
     
  14. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #14
    Dont feel bad OP. I cant get hello world to work either in XCode (I think your using xcode) but my SDL programs work just fine.
     
  15. Dragonlance1561 macrumors member

    Dragonlance1561

    Joined:
    Aug 16, 2007
    #15
    I would say this thread says otherwise: http://forums.macrumors.com/showthread.php?t=705004

    but actually he was just using the gcc compiler with terminal (he just installed xcode for the non-built-in compiler most likely)
     
  16. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #16
    Hahaha! Ok ALL but that portion will work :) (I suspect its quartz related)
     
  17. Ti_Poussin macrumors regular

    Ti_Poussin

    Joined:
    May 6, 2005
    #17
    It's a bad habit, when the project get large and many namespace get involved. But it's shorter for small project.

    You should learn to use a makefile pretty quickly if you don't want to do this every time. Here's a good link for the basic stuff:
    http://www.gnu.org/software/make/manual/make.html
     

Share This Page