Open In App

Compiling with g++

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. 
 

g++ --version


 

// 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 hello.cpp


 

com-only


g++ -c hello.cpp


 

single-c


g++ -o main.exe hello.cpp


 

// hello.cpp file
#include "helloWorld.h"
#include <iostream>
int main()
{
    std::cout << "Hello Geek\n";
    helloWorld();
    return 0;
}


 

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


 

// helloWorld.h file
void helloWorld();


 

g++ -c helloWorld.cpp hello.cpp
g++ -o main.exe helloWorld.o hello.o
./main.exe
// hello.cpp file
#include <iostream>
int main()
{
    int i;
    std::cout << "Hello Geek\n";
    return 0;
}


 

g++ -Wall hello.cpp

warn


 

Article Tags :