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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Public Domain - NO WARRANTY

## Generic GNU Makefile

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: You can compile it under FreeBSD installing the GNU make port /usr/ports/devel/gmake, then: $ 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Public Domain - NO WARRANTY

## Generic FreeBSD Makefile

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: GNUMakefile and BSDMakefile

To compile:

$ gmake -f GNUMakefile
$ make -f BSDMakefile