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 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 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.cppIt 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.oIt links the object codes helloWorld.o and hello.o to create an executable file main.exe ./main.exeIt 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.cppFile 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 Comment More infoAdvertise with us akshatmahla Follow 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 Like