Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

Compiling with g++

Last Updated : 08 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. The different "options" of g++ command allow us to stop this process at the intermediate stage. 
 

  • Check g++ compiler version information:
     
g++ --version


 

  • Compile a CPP file to generate executable target file: g++ file_name command is used to compile and create an executable file a.out (default target name).
    Example: Given a simple program to print "Hello Geek" on standard output with file name hello.cpp
     
CPP
// hello.cpp file #include <iostream> int main() {     std::cout << "Hello Geek\n";     return 0; } 


 

g++ hello.cpp


 


This compiles and links hello.cpp to produce a default target executable file a.out in present working directory. To run this program, type ./a.out where ./ represents present working directory and a.out is the executable target file.
 

./a.out


 

  • g++ -S file_name is used to only compile the file_name and not assembling or linking. It will generate a file_name.s assembly source file.
    Example:
     
g++ -S hello.cpp


 

com-only


  • g++ -c file_name is used to only compile and assemble the file_name and not link the object code to produce executable file. It will generate a file_name.o object code file in present working directory.
    Example:
     
g++ -c hello.cpp


 

single-c


  • g++ -o target_name file_name: Compiles and links file_name and generates executable target file with target_name (or a.out by default).
    Example:
     
g++ -o main.exe hello.cpp


 

  • Compile and link multiple files: When -c flag is used, it invokes the compiler stage which translates source code to object code.When -o flag is used it links object code to create the executable file from file_name.o to a.out(default), multiples files may be passed together as arguments.
    Example:
     
CPP
// hello.cpp file #include "helloWorld.h" #include <iostream> int main() {     std::cout << "Hello Geek\n";     helloWorld();     return 0; } 


 

CPP
// helloWorld.cpp file #include <iostream> void helloWorld() {     std::cout << "Hello World\n"; } 


 

CPP
// helloWorld.h file void helloWorld(); 


 

g++ -c helloWorld.cpp hello.cpp
  • It compiles and creates object code for the files helloWorld.cpp and hello.cpp to helloWorld.o and hello.o respectively.
     
g++ -o main.exe helloWorld.o hello.o
  • It links the object codes helloWorld.o and hello.o to create an executable file main.exe
     
./main.exe
  • It runs the executable file main.exe
     
  • g++ -Wall file_name: It prints all warning messages that are generated during compilation of file_name.
    Example:
     
CPP
// hello.cpp file #include <iostream> int main() {     int i;     std::cout << "Hello Geek\n";     return 0; } 


 

g++ -Wall hello.cpp
  • File extension for c++ files can be .cpp or .c++ , .cpp is widely used but .cpp and .c++ are exactly same and all above functionalities are same for .c++ too

warn


 


A

akshatmahla
Improve
Article Tags :
  • Linux-Unix

Similar Reads

    fdisk command in Linux with examples
    The 'fdisk' command in Linux, short for "format disk," is a powerful, dialog-driven utility used for creating and manipulating the disk partition table. Essential for system administrators and users looking to manage their hard drive’s structure, 'fdisk' supports a range of operations such as creati
    5 min read
    fg command in Linux with examples
    The fg command in Linux is used to bring a background job into the foreground. It allows you to resume a suspended job or a background process directly in the terminal window, so you can interact with it.Syntaxfg [job_spec]The job_spec is a way to refer to the background jobs that are currently runn
    3 min read
    fgrep command in Linux with examples
    The 'fgrep' filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings that contain lots of regular expression metacharacters, such as "^", "$", etc. This makes 'fgrep' particularly v
    4 min read
    file command in Linux with examples
    The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable f
    3 min read
    How to Find a File in Linux | Find Command
    The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
    9 min read
    Finger command in Linux with Examples
    The 'finger' command is a powerful utility in Linux used to display information about users logged into the system. This command is commonly used by system administrators to retrieve detailed user information, including login name, full name, idle time, login time, and sometimes the user's email add
    4 min read
    fmt command in Linux with examples
    fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specif
    4 min read
    fold command in Linux with examples
    The fold command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output. By default, it wraps lines at a maximum width of 80 columns, which is configurable. To fold input using the fold command pass a file or standard input to the command. Syntax of `
    3 min read
    for command in Linux with Examples
    IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain.Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of e
    2 min read
    free Command in Linux with examples
    While using LINUX there might come a situation when you are willing to install a new application (big in size) and you wish to know the amount of free memory available on your system. In LINUX, there exists a command line utility for this and that is the 'free' command which displays the total amoun
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences