It seems to me that gcc can deal with both c and c++ projects,so why is g++/gcc-c++ needed?
What's the difference between g++ and gcc-c++?
gcc is used to compile C program while g++ is used to compile C++ program. Since, a C program can also be compile complied through g++, because it is the extended or we can say advance compiler for C programming language.
The gcc -g flag tells gcc to generate and embed debug information. ulimit -c is used to enable core file generation. You can have either of these without the other. Copy link CC BY-SA 2.5.
GNU C++ Compiler ( g++ ) is a compiler in Linux which is used to compile C++ programs. It compiles both files with extension . c and . cpp as C++ files.
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.
gcc
will compile C source files as C and C++ source files as C++ if the file has an appropriate extension; however it will not link in the C++ library automatically.
g++
will automatically include the C++ library; by default it will also compile files with extensions that indicate they are C source as C++, instead of as C.
From http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:
C++ source files conventionally use one of the suffixes
.C
,.cc
,.cpp
,.CPP
,.c++
,.cp
, or.cxx
; C++ header files often use.hh
,.hpp
,.H
, or (for shared template code).tcc
; and preprocessed C++ files use the suffix.ii
. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and treats
.c
,.h
and.i
files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library. This program is also useful when precompiling a C header file with a.h
extension for use in C++ compilations.
For example, to compile a simple C++ program that writes to the std::cout
stream, I can use either (MinGW on Windows):
But if I try:
I get undefined references at link time.
And for the other difference, the following C program:
#include <stdlib.h> #include <stdio.h> int main() { int* new; int* p = malloc(sizeof(int)); *p = 42; new = p; printf("The answer: %d\n", *new); return 0; }
compiles and runs fine using:
But gives several errors when compiled using:
Errors:
test.c: In function 'int main()': test.c:6:10: error: expected unqualified-id before 'new' test.c:6:10: error: expected initializer before 'new' test.c:7:32: error: invalid conversion from 'void*' to 'int*' test.c:10:9: error: expected type-specifier before '=' token test.c:10:11: error: lvalue required as left operand of assignment test.c:12:36: error: expected type-specifier before ')' token
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With