Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between g++ and gcc?
The gcc and g++ are both GNU compiler collection tools, but they serve different purposes. gcc is primarily designed for C programs, while g++ is designed for C++ programs. Understanding their differences helps choose the right compiler for your project.
Syntax
gcc program.c -o executable_name g++ program.cpp -o executable_name
gcc (GNU C Compiler)
The gcc compiler is specifically designed to compile C programs. It treats files with .c extension as C source code and links against the standard C library by default.
Example: Using gcc for C Program
#include <stdio.h>
int main() {
int a = 20;
printf("The value of a: %d\n", a);
return 0;
}
The value of a: 20
Compilation: Save the code as
program.cand compile using:gcc program.c -o program
g++ (GNU C++ Compiler)
The g++ compiler is designed for C++ programs. It can compile both .c and .cpp files, but treats them as C++ source code and links against the C++ standard library.
Example: Using g++ for C++ Program
#include <stdio.h>
int main() {
int a = 20;
printf("The value of a: %d\n", a);
return 0;
}
The value of a: 20
Compilation: Save the code as
program.cppand compile using:g++ program.cpp -o program
Key Differences
| Aspect | gcc | g++ |
|---|---|---|
| Primary Use | C programs | C++ programs |
| File Extensions | .c files | .cpp, .cxx, .cc files |
| Default Libraries | C standard library | C++ standard library |
| Language Features | C language features only | Full C++ features (classes, objects, etc.) |
Conclusion
Use gcc for pure C programs and g++ for C++ programs. While g++ can compile C code, gcc is more appropriate for C-only projects as it provides better optimization and avoids unnecessary C++ library linking.
