Compiling with g++ (original) (raw)

Last Updated : 8 Apr, 2024

**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 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 int main() { std::cout << "Hello Geek\n"; helloWorld(); return 0; }

CPP

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

CPP

// 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 int main() { int i; std::cout << "Hello Geek\n"; return 0; }

`

g++ -Wall hello.cpp

warn