Recently I had to port some project from Debian (GNU/Linux) to FreeBSD. I use GNU Make to compile it in Debian, but in FreeBSD, I prefer to use the default make of the system.

I customize this generic Makefile for my projects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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) $<

FreeBSD has a GNU Make port /usr/ports/devel/gmake, to use it:

$ gmake

but in FreeBSD I like to utilize the system default Make.
FreeBSD Make does not recognize some GNU Make Automatic Variables and each tool wants a different bracket type so we cannot use the same Makefile instantly, 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 the two Makefile in the same directory so I name them: GNUMakefile and BSDMakefile

In FreeBSD shell:

$ gmake -f GNUMakefile
$ make -f BSDMakefile