What is g++?

Discussion in 'Mac Programming' started by Soulstorm, Jun 14, 2005.

  1. Soulstorm macrumors 68000


    Feb 1, 2005
    This may sound like a newbish question, but what is g++? It is required to use this command when compiling with the teminal a c++ file.

    But I haven't seen that only as a reference to a terminal command, I have also seen it somewhere else, although I can't remember where.

    What is it?
  2. cubist macrumors 68020

    Jul 4, 2002
    Muncie, Indiana
    Gnu C compiler (gcc) C++.

    I think this is old terminology, you just use gcc for .c and .cpp files now.
  3. superbovine macrumors 68030


    Nov 7, 2003
    nope, doesn't that way. g++ must be used with C++ code.

    -- bar.c --
    #include <iostream>
    using namespace std;
    int main() 
        cout << "Hello World" << endl;
        return 0;
    from terminal

    superbovine:cow ~/foo $ gcc bar.c -o bar
    bar.c:1:20: error: iostream: No such file or directory
    bar.c:3: error: parse error before 'namespace'
    bar.c:3: warning: data definition has no type or storage class
    bar.c: In function 'main':
    bar.c:8: error: 'cout' undeclared (first use in this function)
    bar.c:8: error: (Each undeclared identifier is reported only once
    bar.c:8: error: for each function it appears in.)
    bar.c:8: error: 'endl' undeclared (first use in this function)
    superbovine:cow ~/foo $ g++ bar.c -o bar
    superbovine:cow ~/foo $ ./bar
    Hello World
    superbovine:cow ~/foo $ 
  4. slb macrumors 6502

    Apr 15, 2005
    New Mexico
  5. DXoverDY macrumors 6502a

    Apr 19, 2005
    correct. gcc does it all, the g++ executable is simply for backwards compatibility i believe.
  6. Soulstorm thread starter macrumors 68000


    Feb 1, 2005
    Actually, I think that pressing "gcc" will not work with c++ files.
  7. DXoverDY macrumors 6502a

    Apr 19, 2005
    works perfectly fine for me man, xcode 2.1

    open terminal..

    cd /usr/bin/
    ls -l g++*
    ls -l gcc*

    notice anything similar about each?

    i was wrong that they aren't symbolic links, but regardless, they're the same file. and again, my c++ files compile just fine with gcc or g++

    what's even more funny is that you asked in this thread what gcc is... and now you're the expert.. lol.. :rolleyes:
  8. AlmostThere macrumors 6502a

    Use gcc -x c++ to compile c++ files.
    g++ is simply a frontend that brings in all the stuff required to compile c++. All part of the Gnu Compiler Collection
  9. iMeowbot macrumors G3


    Aug 30, 2003
    No, but the real differences in g++ and gcc are very small, simply defaulting to a different architecture type (which in turn makes a very big difference in how the compiler and linker are run).
  10. SamMiller0 macrumors member


    Aug 17, 2004
    San Jose, CA
    On the version of gcc I use (3.3.5 on Debian) you still have to link with the stdc++ library or you get linker errors.

    gcc -x c++ -lstdc++ -o foo foo.cpp
  11. Loge macrumors 68020


    Jun 24, 2004
    This is the same on Mac OS X. With g++ you don't need to remember the name of the library, which is just as well because I usually misspell it.
  12. AlmostThere macrumors 6502a

    Very much so. My previous post was far from clear :(

    gcc -x c++ is only for compilation and does not invoke the linker with libstdc++ (which g++ does).

    Also, gcc will compile the above example as c++ anyway, if foo.cpp was renamed foo.c then -x c++ is required to override the default behaviour which is based on file extension:

    gcc -c -x c++ foo.c

Share This Page