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

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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?
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
cubist said:
Gnu C compiler (gcc) C++.

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

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

-- bar.c --
Code:
#include <iostream>

using namespace std;

int main() 
{

    cout << "Hello World" << endl;
    return 0;
}

from terminal

Code:
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 $
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
DXoverDY said:
correct. gcc does it all, the g++ executable is simply for backwards compatibility i believe.
Actually, I think that pressing "gcc" will not work with c++ files.
 

DXoverDY

macrumors 6502a
Apr 19, 2005
810
0
Soulstorm said:
Actually, I think that pressing "gcc" will not work with c++ files.

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:
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
slb said:
g++ is a symbolic link to gcc, IIRC.
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).
 

SamMiller0

macrumors member
Aug 17, 2004
66
0
San Jose, CA
AlmostThere said:
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

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
 

Loge

macrumors 68030
Jun 24, 2004
2,821
1,310
England
SamMiller0 said:
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

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.
 
SamMiller0 said:
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

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.