What is the difference between g++ and gcc? Which ones should be used for general c++ development?
Join Stack Overflow to learn, share knowledge, and build your career.
|
Even though they automatically determine which backends ( The probably most important difference in their defaults is which libraries they link against automatically. According to [1] and [2], |
|||||||||
|
|
GCC: GNU Compiler Collection
gcc: GNU C Compiler The main differences:
Extra Macros when compiling *.cpp files:
|
|||||||||||||||||||||
|
|
For c++ you should use g++. It's the same compiler (e.g. the GNU compiler collection). GCC or G++ just choose a different front-end with different default options. In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries. The gcc frontend won't do that (also it could link with them if you pass the right command line options). |
|||
|
|
|
Although the gcc and g++ commands do very similar things, g++ is designed to be the command you'd invoke to compile a C++ program; it's intended to automatically do the right thing. Behind the scenes, they're really the same program. As I understand, both decide whether to compile a program as C or as C++ based on the filename extension. Both are capable of linking against the C++ standard library, but only g++ does this by default. So if you have a program written in C++ that doesn't happen to need to link against the standard library, gcc will happen to do the right thing; but then, so would g++. So there's really no reason not to use g++ for general C++ development. |
|||
|
|
|
“GCC” is a common shorthand term for the GNU Compiler Collection. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”). When referring to C++ compilation, it is usual to call the compiler “G++”. Since there is only one compiler, it is also accurate to call it “GCC” no matter what the language context; however, the term “G++” is more useful when the emphasis is on compiling C++ programs. You could read more here. |
||||
|
|
|
I became interested in the issue and perform some experiments
p.s. Test files
Findings:
|
||||
|
|
|
The only notable difference is that i you pass a .c to gcc it will compile as C, whereas g++ will always treat it as C++ |
|||||
|
|
What is the difference between
The For example on Ubuntu 16.04, The
and that the default ...
Search the Which one should be used for general c++ development? Either |
||||
|
|
|
gcc and g ++ are both GNU compiler. They both compile c and c++. The difference is for *.c files gcc treats it as a c program, and g++ sees it as a c ++ program. *.cpp files are considered to be c ++ programs. c++ is a super set of c and the syntax is more strict, so be careful about the suffix. |
|||||
|
|
I was testing gcc and g++ in a linux system. By using MAKEFILE, I can define the compliler used by "GNU make". I tested with the so called "dynamic memory" locating feature of "C plus plus" by :
Only g++ can successfully compile on my computer while gcc will report error
So my own conclusion is gcc does not fully support "C plus plus". It seems that choosing g++ for C++ source files is a better option. |
|||||
|