To only compile a file
gcc -c filename.c
For C++
g++ -c filename.cpp
To debug a file
gcc -g filename.c
To add a #define macro without editing the source file
g++ -c -D HELL=6 filename.c
To link the file and create the executable fil(Unlike Windows environment,executable files here dont end with .exe
.They dont have any extension
gcc -o filename filename.o
OR
g++ -o filename filename.o
here filenmae.o is the object file
If we want to compile and link multiple files, we need to use make
We need to create the Makefile file first in order to use make.The Makefile looks like this:-
hello:hello.o
gcc -o hello hello.o
hello.o: hello.c
gcc -c hello.c
clean :
rm -f *.o hello
There need to be a tab before the next line begins
The Makefile ,c/c++ file ,object files should be under the same directory
Now give the command
$make
gcc -c hello.c
gcc -o hello hello.o
The Commands are executed as Makefile has specified.
Now to clean up the redundant files ,use the command
$make clean
rm -f *.o hello
To add flags without editing the Makefile
$make CFLAGS=-02 (02 optimises the code so it runs as quickly as possible)
OR
$make CFLAGS=-g
gcc -c -g hello.c
gcc -o -g hello hello.o
To execute the executable file ,use the commands
$./filename