Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between gcc and g++/gcc-c++?

Tags:

gcc

g++

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

like image 991
gdb Avatar asked May 02 '11 05:05

gdb


People also ask

Should I use GCC or G ++ for 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.

What is G in GCC?

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.

What does G ++ mean in C?

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.

What is the difference between g ++ and C++?

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.


1 Answers

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):

  • g++ -o test.exe test.cpp
  • gcc -o test.exe test.cpp -lstdc++

But if I try:

  • gcc -o test.exe test.cpp

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:

  • gcc -o test.exe test.c

But gives several errors when compiled using:

  • g++ -o test.exe test.c

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 
like image 148
Michael Burr Avatar answered Oct 09 '22 04:10

Michael Burr