Home » C programming

What is the difference between gcc and g++ in Linux?



Difference between gcc and g++

Both are the compilers in Linux to compile and run C and C++ programs. Initially gcc was the GNU C Compiler but now a day's GCC (GNU Compiler Collections) provides many compilers, two are: gcc and g++.

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.

Compiler command to compile C program through gcc

gcc  program.c  -o binary

program.c is the C source file name and binary is the name of binary (object file) that will be executed.

Compiler command to compile C++ program through g++

g++  program.cpp  -o binary

program.cpp is the C++ source file name and binary is the name of binary (object file) that will be executed.

C Example (main.c) - Compile, Run through gcc

/* main.c */
 
#include <stdio.h>
 
int main()
{
    printf("Hello, World!\n");
 
    return 0;
}

Output

    sh-4.3$ gcc main.c -o main

    sh-4.3$ ./main
    Hello, World!


C++ Example (main.cpp) - Compile, Run through g++

/* main.cpp */
 
#include <iostream>
 
using namespace std;
 
int main()
{
   cout << "Hello World" << endl; 
    
   return 0;
}

Output

    sh-4.3$ g++ main.cpp -o main

    sh-4.3$ ./main
    Hello, World!





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.