"g++" and "c++" compiler - c++

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?

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.

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.

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++.

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.

Related

Relationship between gcc, g++, cygwin, and wingw?

I know for my class, I had to install cygwin to get my Netbeans IDE running, but I see options during setup for both g++ and gcc and I am not sure if they are the same thing or not, and where does wingw? is it another compiler, and if so why choose on over the other?
g++ and gcc are the gnu C++ and C compiler respectively. They're really the same compiler with different flags though.
MinGW is "Minimalist Gnu for Windows". It's a port of the gnu compiler to run on Windows.
Cygwin is another port of the gnu compiler (and various other utilities) to Windows. More accurately (IMO, anyway), it's leaving the compiler/programs running on POSIX, and porting a POSIX layer to run on Windows.
As to choosing between them: if you're running Linux, you probably want ot just get a package of gcc/g++ for the distro you're using.
If you're running Windows, it'll depend on your intent. Cygwin works well for porting existing Linux/POSIX code to Windows. If, however, you plan to write code, and just want a compiler, I'd go for MinGW instead.
One other note: the MinGW at MinGW.org hasn't been updated in years. If you decide to go with MinGW, I'd advise getting it from nuwen.net instead (it's updated quite regularly).
gcc will compile: .c/.cpp files as C and C++ respectively.
g++ will compile: .c/.cpp files but they will all be treated as
C++ files.
Also if you use g++ to link the object files it automatically
links in the std C++ libraries (gcc does not do this).
gcc compiling C files has less predefined macros.
For wingw, did you mean mingw? Because MinGW is for compatibility with Windows. MinGW uses GCC/G++, and MinGW is not a compiler, it's basically a stripped version of Cygwin that uses MS libs wherever possible.
Depends upon what you are building;
gcc is for C programs.
g++ is for C++ programs.
I've not heard of wingw.

Is g++ compiler the same with the gcc compiler for C++? (Mac OS X)

I just need to make clear one thing. In University we are learning the C++ programming language and they suggest us to use the GNU C++ Compiler which is part of the GCC.
So on my Mac OS X Mavericks I download the command line tools from the developers.apple.com.
I wrote a simple C++ program and I compile this program using the g++ command like this:
g++ program.cpp
./a.out
And the program runs perfect. But as I know, using a different compiler, means that you have to use the correct syntax/commands/libraries for this spesific compiler, so while in the University we use the "GNU C++ compiler", I just want to make clear that with the g++ command is meant that I use the "GNU C++ Compiler".
Cheers.
Traditionally, gcc and g++ are both components of the GNU C compiler suite. gcc is the C compiler, and g++ is the C++ compiler.
On current versions of Mac OS X, the commands gcc and g++ are both treated as alternate names for clang and clang++, which are components of the Clang C compiler. However, this compiler is almost entirely compatible with GCC — the few differences that do exist will almost certainly not come up in the coursework you're doing.
(The most significant difference is that Clang's diagnostics are much better: it will point out exactly where a syntax error occurs in a line, rather than just what line it's on, and it can often identify potential typos or subtle mistakes in situations where GCC would just give you a cryptic error message. If you're just learning C, you will appreciate this a lot.)
As per #duskwuff, I would prefer to use clang++, however if you must be compatible, then you can installi the real GNU compiler via macports.
After installing macports (which includes a xcode-select step), simply do:
$ sudo port selfupdate
$ sudo port install gcc46
(or gcc47, etc.)
The compiler will be in your $PATH (if you set-up macports correctly), but explicitly, it will be /opt/local/bin/gcc46 (see sudo port select gcc).

What's the relationship between binutils and gcc?

As titled, is binutils contained in gcc for Centos Linux?
If I install gcc rpm package, is there need to install binutils also?
What's more, are gcc and g++ both installed by default in Centos?
The gcc package probably contains the compiler proper, e.g. files /usr/bin/gcc and directory /usr/lib/gcc/x86_64-linux-gnu/4.8/ (which contains the cc1 executable).
The /usr/bin/gcc program starts cc1 (or cc1plus etc...) to compile your source code *.c, and also as to translate cc1-generated assembly code (produced by cc1) into object file *.o, and at last ld to link.
Compile once with gcc -v to understand what is happening, it would show the really executed binaries. Notice that gcc is only a driving program (starting other executables like cc1, as, ld ...)
The as and ld programs are provided by binutils -which is needed to compile.
So the binutils package is a required dependency for the gcc package (with many other dependencies, probably including libc and libc-devel, but if you really want you could use some other libc like MUSL libc; the libc is generally providing the dynamic linker like /lib/ld-linux.so*).
Learn how to use rpm (on Centos, or dpkg on Ubuntu & Debian) to query the dependencies between packages.
For development you probably want some other packages. Debian has the build-essential virtual package. Probably CentOS has an equivalent. And you'll surely want to use some libraries (and you want the development packages for them, e.g. on Debian libcurl4-gnutls-dev to develop with the libcurl HTTP client library). See also this answer (for Ubuntu and Debian, but you can adapt it for CentOS).
In 2021 you want to use GCC 10 at least, as g++ -Wall -Wextra -g and you could decide to code your own GCC plugin (checking some coding rules in your C++ code; you also want to document your coding conventions by writing). Be aware of the rule of five.

What is the difference between g++ and c++? [duplicate]

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?
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.
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.
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++.
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.

install g++ and compile issues

Can I use gcc to compile C++ program? Is there any difference with using g++ instead?
On an exisitng system, I type "man g++", the system returns "no manual entry for g++". But it has gcc installed. Is that possible to install g++ from existing gcc library directly; or I have to download g++ instead?
You can use gcc to compile C++ programs as long as you set the proper flags and/or use the right extensions on your source-files (i.e., use .cpp), but that's only if the C++ development tools, such as libstdc++ have been installed ... and that typically means that g++ is available as well. So if you don't have the C++ development tools and libraries installed, then no, you're not going to be able to compile C++ files, especially if you start using elements from the STL, etc.
That being said, if you restricted your C++ source-code to a strict C-subset of the language, then I don't see why it wouldn't compile ... but that would defeat the whole point of using C++ in the first place ... it would just be a C file with a .cpp extension :-)
If you are on Linux, then installing g++ is fairly straight-forward. Simply search the repository for g++ using apt-get, yum, or some other package manager, and it should come up as a nice pre-compiled binary, along with all it's dependencies. If you are on OSX, then it comes with the Xcode developer tools. If you are on Windows, then you can use MinGW-w64, and again, that comes as a pre-compiled binary for x86_64, or you could use the version that comes with Cygwin (you didn't mention your platform, so it's possible you could be running Cywin on Windows). For that, just use the Cygwin package manager again.
On most systems you will need to install g++ (along with libstdc++ etc) to compile C++ code.
gcc is a frontend that can be instructed to compile C++, but that usage is not recommended. Use g++ which also adds the C++ library etc pp.
/tmp$ cat hw.cc
#include <iostream>
int main(void) {
std::cout << "Hello, world\n" << std::endl;
}
$ g++ -o hw1 hw.cc
$ gcc -o hw2 hw.cc -lstdc++
$ ./hw1
Hello, world
$ ./hw2
Hello, world
$
Just because you can use gcc does not mean you should.
g++ is an extension of gcc. you need to install g++ specific parts to be able to compile c++ programs.