DEV Community

Deepanshu Udhwani
Deepanshu Udhwani

Posted on • Edited on

How to write the Simplest MAKE file

How to make a MAKE file easiest possible way

Make 3 files hellomake.c hellofunc.c hellomake.h

#include <hellomake.h>

int main() { // call a function in another file myPrintHelloMake();

return(0); }
Enter fullscreen mode Exit fullscreen mode

hellofunc.c

#include <stdio.h> #include <hellomake.h>

void myPrintHelloMake(void) {

printf("Hello makefiles!\n");

return; }
Enter fullscreen mode Exit fullscreen mode

hellomake.h

void myPrintHelloMake(void);
Enter fullscreen mode Exit fullscreen mode

After this make a file
vi makefile

CC=gcc CFLAGS=-I. DEPS = hellomake.h OBJ = hellomake.o hellofunc.o

%.o: %.c $(DEPS) 
    $(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ) 
    $(CC) -o $@ $^ $(CFLAGS)
Enter fullscreen mode Exit fullscreen mode

Or
makefile as

IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR =../lib

LIBS=-lm

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = hellomake.o hellofunc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
pentacular profile image
pentacular

You should use

#include "hellomake.h"

Given that hellomake.h is not a system header. :)

It may fail otherwise on some systems.

Collapse
 
itsdeepanshu profile image
Deepanshu Udhwani

Yeah true, I mean create one file named hello.h! 😂😂😂