GNU Make vs BSD Make

Posted on January 28, 2017

Recently I ported some project from Debian to FreeBSD. I used GNU Make to compile under Debian, but in FreeBSD, I preferred to use its make.

This is a generic GNU make Makefile:

# Generic GNU Makefile
# Public Domain - NO WARRANTY

CC = gcc
CCFLAGS = -Wall -g
INCLUDEDIR = ./
LDFLAGS = -lm
OUTPUT = a.out
SOURCES = file1.c file2.c
OBJECTS=$(SOURCES:.c=.o)

all : $(OUTPUT)

clean:
	rm -f $(OUTPUT) *.o *~

$(OUTPUT): $(OBJECTS)
	    $(CC) $(LDFLAGS) $^ -o $@

%.o: %.c
	    $(CC) -I$(INCLUDEDIR) -c $(CCFLAGS) $<

[Note: we can use it under FreeBSD installing the GNU make port, devel/gmake, and running:
%> gmake Makefile]

FreeBSD Make does not recognize some GNU Make Automatic Variables and traditionally (not mandatory) FreeBSD makefiles use curly brackets, we have to change:

$(...)

with:

${...}

and

$(OUTPUT): $(OBJECTS)
	    $(CC) $(LDFLAGS) $^ -o $@

%.o: %.c
	    $(CC) -I$(INCLUDEDIR) -c $(CCFLAGS) $<

with:

${OUTPUT}: ${OBJECTS}
	${CC} ${LDFLAGS} ${OBJECTS} -o ${OUTPUT}

.c.o:
	${CC} -I${INCLUDEDIR} ${CCFLAGS} -c ${.IMPSRC} -o ${.TARGET}

The complete FreeBSD Makefile is below, you could use it as a template for your projects

# Generic FreeBSD Makefile
# Public Domain - NO WARRANTY

CC = clang
CCFLAGS = -Wall -g
INCLUDEDIR = ./
LDFLAGS = -lm
OUTPUT = a.out
SOURCES = file1.c file2.c
OBJECTS=${SOURCES:.c=.o}

all : ${OUTPUT}

clean:
	rm -f ${OUTPUT} *.o *~

${OUTPUT}: ${OBJECTS}
	${CC} ${LDFLAGS} ${OBJECTS} -o ${OUTPUT}

.c.o:
	${CC} -I${INCLUDEDIR} ${CCFLAGS} -c ${.IMPSRC} -o ${.TARGET}


Sometimes I need to have two Makefile in the same directory, usually I name them: GNUMakefile and BSDMakefile, to compile:

%> gmake -f GNUMakefile
%> make -f BSDMakefile