What the...!? I thought GCC was better.

Discussion in 'Mac Programming' started by BobXRoberts, Mar 29, 2006.

  1. BobXRoberts macrumors newbie

    Mar 29, 2006
    I'm using the version of gcc installed from the Tiger Developer Tools outfit. I try to compile this simple C++ code:

    #include <iostream>
    using namespace std;

    int main ()
    int i;
    cout << "Please enter an integer value: ";
    cin >> i;
    cout << "The value you entered is " << i;
    cout << " and its double is " << i*2 << ".\n";
    return 0;

    with gcc, using no special flags or suchlike. I get the following errors:

    /usr/bin/ld: Undefined symbols:
    std::basic_istream<char, std::char_traits<char> >::eek:perator>>(int&)
    std::basic_ostream<char, std::char_traits<char> >::eek:perator<<(int)
    std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
    collect2: ld returned 1 exit status

    What's going on? Does gcc not support ANSI C++? I think not. I am stumped. Any help would be greatly appreciated :D
  2. gekko513 macrumors 603


    Oct 16, 2003
    g++ test.cpp -o test
    macbook:~/Utvikling audun$ ./test
    Please enter an integer value: 4
    The value you entered is 4 and its double is 8.

    Make sure you use g++. (g++ automatically links in the standard c++ libraries)
  3. BobXRoberts thread starter macrumors newbie

    Mar 29, 2006
    Cheers mate! I didn't know there were any libraries needed apart from iostream... I'm only just getting into C++, my faith in gcc is restored (after many happy experiences with C!) What _are_ the standard libraries btw?
  4. gekko513 macrumors 603


    Oct 16, 2003
    I'm talking about the iostream stuff. If you compile with just gcc, it will compile fine because the header for iostream is included, but it fails on linking because the actual object files with the implementation of those functions aren't linked into the binary.

    g++ lets you not worry about that linking stuff. I'm not even sure exactly what it links in because g++ has made me not worry :cool:, but I know you can compile the same thing with gcc if you include the correct options.

  5. mrichmon macrumors 6502a

    Jun 17, 2003
    Yup. The "gcc" command links in only the basic C libraries. "g++" links in the basic C++ libraries.

    In general, you can just think that "gcc" is a C compiler, and "g++" is a C++ compiler and ignore the finer details.
  6. bousozoku Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    Really? The cc in gcc is compiler collection. You should be able to compile FORTRAN or COBOL or anything else with the correct pieces in place.

    Generally, though, I build in Xcode and run at the shell prompt. It avoids the mess. If I build at the shell prompt, I use make. :)
  7. zimv20 macrumors 601


    Jul 18, 2002
    but what does make call? gcc or g++?
  8. mrichmon macrumors 6502a

    Jun 17, 2003
    Originally "gcc" stood for "GNU C Compiler". "g++" was then the GNU C++ compiler.

    GCC forked into EGCS "Enhanced GNU Compiler System". EGCS and GCC developed in parallel for a while. (I certainly was using GCC and EGCS for various reasons around 1995/1996.) In 1999 EGCS was adopted as the official version of GCC and this was when the name was changed from "GNU C Compiler" to "GNU Compiler Collection".

    When you install the GNU project Fortran 77 compiler you get the command "g77" (GNU Fortran 77) to compile Fortran code.

    When you install the GNU Java compiler you get the command "gcj".

    You can certainly use the "gcc" compiler to compile C++ and in a small number of cases where you need to mess about with linking both the C and C++ basic libraries and other related hijinx.

    In general, for clarity, and to avoid the occassional obscure build issues giving you grief, you are better off just using the dedicated commands.
  9. savar macrumors 68000


    Jun 6, 2003
    District of Columbia
    Everybody else gave you good advice, but I thought I should add one more thing: The error you're getting is actually from 'ld', not directly from gcc. ld is the linker, and its telling you it couldnt find a number of symbols referenced from your program. If you want to link programs manually, this is the command you would use. (I imagine this is also how XCode links)
  10. mrichmon macrumors 6502a

    Jun 17, 2003
    make calls whatever the author of the Makefile wrote it to call.
  11. zimv20 macrumors 601


    Jul 18, 2002
    well, yes, of course. i was asking bousozoku what he specified.
  12. BobXRoberts thread starter macrumors newbie

    Mar 29, 2006
    Ha I seem to have started a right discussion. I suppose I can get away with ignoring the details for now- I just like to know how it works, right down to the wire. That's the silly thing about how we learn programming and comp sci in general today- we focus on individual layers and neglect how they all connect together...

    As we're here, does anyone know of a basic (and I mean basic, I'm trying to get into graphics) OS X openGL tutorial? I could go and buy a book. But I'm broke. :d
  13. Soulstorm macrumors 68000


    Feb 1, 2005
    I think that if you go to www.opengl.org , you can find the the very early versions of Blue Book (reference of OpenGL) and Red Book (Programming Guide) in order to start with OpenGL.

    Note that in both books, it is taken for granted the fact that you must know how to set up your environment for OS X. It's no big deal though. Just tell me to help you if you have any problems.
  14. BobXRoberts thread starter macrumors newbie

    Mar 29, 2006
    Thanks, everyone! I'm having a look around... very interesting it is too.

Share This Page