Jul 7, 2017

Writing a makefile for a linux project


 Say we have files main.c hello.c math.c and math.h
Suppose main calls hello() and then math()

gcc Steps

Preprocess
1. Removes all preprocessor directives (e.g: #define etc)
2. produces .i files from .c files
        main.i hello.i
Compile
1. Converts to assembly
2. Produces main.s hello.s math.s

Assembling
1.Converts to machine language with unresolved directives
2. Produces main.o hello.o and math.o binaries

Link
gcc -o main main.o hello.o math.o -lc
1.Creates machine language executable
2.Produces main binary

Useful shortcuts

make all : creates the final binary
make clobber : delete all temp files, core files, binaries, etc.
make clean : delete all binaries

files ending ~ and starting or ending in # are temp files
core is a file produced when a program dumps core



Libraries

Libraries are files of ready-compiled code that compiler links with the c programs during linker stage.




Followers