68

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?

2

4 Answers 4

73

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.

1
  • 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++.) Oct 7, 2015 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.

8
  • 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, 2012 at 8: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, 2012 at 9: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, 2012 at 9:34
  • @Claudio, oops, right, forgot about that. Then this can be not only imprecise answer, but wrong altogether. Jun 27, 2012 at 9:36
  • @Claudio, And on BSD systems it's almost surely wrong. I'm confused now, I have to revise the answer. Jun 27, 2012 at 9:43
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++.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.