72

I just found on my Ubuntu, there are two different C++ compilers: /usr/bin/g++ and /usr/bin/c++. I am not familiar with the latter, but man c++ just jumps to the manpage of gcc. I wonder what is their difference as C++ compilers?

Countour-Integral
  • 1,138
  • 2
  • 7
  • 21
Tim
  • 1
  • 141
  • 372
  • 590
  • 8
    `cc` and `c++` are names of POSIX compilers. More likely than not, `c++` will be a symlink to `g++`, and `cc` to `gcc`. – Cat Plus Plus Jun 27 '12 at 08:50
  • Possible duplicate of [What is the difference between g++ and gcc?](https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc) – Romain Vincent Sep 11 '17 at 20:02

4 Answers4

79

This is typical Ubuntu symlink mayhem.

If you ls -l /usr/bin/c++, you will see it is actually a symbolic link. to:

/etc/alternatives/c++

Which in turn, is also a symbolic link to:

/usr/bin/g++

So, on Ubuntu systems, c++ is g++. The reasoning behind the link indirection is that there are multiple packages that could provide a c++ compiler (such as different versions of g++). You'll see this a lot on Ubuntu. For example, qmake is a link to a file in /etc/alternatives, which is (on my system) a link back to /usr/bin/qmake-qt3.

MichaelM
  • 5,518
  • 2
  • 30
  • 23
  • 3
    on Fedora 21, /usr/bin/c++ and /usr/bin/g++ are both binary files (i.e. not symlinks)... but the two files are identical (same number of bytes and same md5sum). (A symlink seems more appropriate... not sure why they did a copy of the same file. Both g++ and c++ are provided by the same rpm `gcc-c++`.) – Trevor Boyd Smith Oct 07 '15 at 12:37
35

c++ is a standard name of a C++ compiler on a system.

On a GNU system you almost surely have GCC (GNU compiler collection) installed, which includes a C++ compiler named g++ ('g' for GNU). But to be POSIX-compatible, they install this compiler as c++ also, sometimes c++ is a symbolic link to g++ sometimes it's a hard link, sometimes it's just the same file installed twice.

This can be not the case for other systems like FreeBSD or NetBSD. It's possible that those systems don't have GCC (and other GNU stuff) installed.

On my system these two files are just identical:

% diff `which c++` `which g++`
% echo $?
0

This means that c++ at least invokes the same compiler, but theoretically it can interpret some command line options differently or have some different defaults. Someone with more knowledge is free to extend the answer in this regard.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
  • 1
    thanks i check md5sum of c++ and g++ and they are same! But what for here 2 files that make same job? – Laser Jun 27 '12 at 08:54
  • Yep, there is no difference, and you can use whichever you like. If you use GCC specific options in your build then I would recommend `g++`, just so it's clear, but you should always let the user override the compiler with the `CXX` variable. – ams Jun 27 '12 at 09:00
  • 2
    Even if the files are identical it doesn't mean they are equivalent. A program may behave differently depending on what name was used to call it (for example `gunzip` and `zcat` are link to `gzip`, and different default arguments are used when `gzip` is called with the other two names). – Claudio Jun 27 '12 at 09:34
  • @Claudio, oops, right, forgot about that. Then this can be not only imprecise answer, but wrong altogether. – unkulunkulu Jun 27 '12 at 09:36
  • @Claudio, And on BSD systems it's almost surely wrong. I'm confused now, I have to revise the answer. – unkulunkulu Jun 27 '12 at 09:43
  • @Claudio, changed the answer to be correct at least, but still don't know the real difference, do you? – unkulunkulu Jun 27 '12 at 12:46
  • @unkulunkulu No idea if there is any difference. – Claudio Jun 27 '12 at 13:47
  • FreeBSD 9 has gcc installed, but it will be removed for most platforms in FreeBSD 10. – Konrad Borowski Dec 08 '13 at 12:35
11

On my machine, c++ is a link:

$ readlink /usr/bin/c++
/etc/alternatives/c++
$ readlink /etc/alternatives/c++
/usr/bin/g++

So c++ is just a link to g++.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
6

g++ is the gnu c++ compiler where c++ is the system c++ compiler, in the case of ubuntu C++ is a link to g++ however in another system it could very well be a link to a non gcc compiler. as someone else said vi vs vim. just because a link to vi exists on the system doesn't mean that it's vim could be any vi clone.

xenoterracide
  • 16,274
  • 24
  • 118
  • 243