5

Please note that this is not a duplicate of the other questions named generic makefile.

I have followed all of the instructions on other questions about generic makefiles, and this is the code I have come up with from that:

CFLAGS = -c
CC = cc
SOURCES = $(wildcard *.cc)
OBJECTS = $(patsubst %.cc,%.o,%(SOURCES))
EXEC = run

all: build clean

build: $(OBJECTS)
    $(CC) $(OBJECTS) -o $(EXEC)

%.o: %.cc
    $(CC) $(CFLAGS) $<

clean:
    rm *.o

However, when I execute make with a file called test.cc in my directory, it gives me the followig error:

cc    -o run
cc: error: no input files
*** Error code 1

Stop.
make: stopped in /somewhere

Please note that I am on FreeBSD and the make and cc commands are the ones which come with the OS.

reinierpost
  • 8,425
  • 1
  • 38
  • 70
Ethan McTague
  • 2,236
  • 3
  • 21
  • 53
  • Note: If the *.cc files contain C++, you should use a C++ compiler. `CC = cc` is very likely a C compiler. – Jens Feb 01 '15 at 14:46

3 Answers3

6

The lines

SOURCES = $(wildcard *.cc)
OBJECTS = $(patsubst %.cc,%.o,%(SOURCES))

are GNU make syntax, not understood by FreeBSD's make, which has its own dialect (specifically $(wildcard) and $(patsubst)). If you need to write makefiles portable to many systems, either require gmake to exist and use GNUmakefiles, or stick to the features of POSIX make.

You can install GNU make (gmake) on FreeBSD with

cd /usr/ports/devel/gmake
make install clean
Jens
  • 69,818
  • 15
  • 125
  • 179
4

In FreeBSD's make, you can do it like this:

SOURCES!= ls *.cc
OBJECTS = ${SOURCES:.cc=.o}

The first line uses a variable assignment modifier. Expand the value and pass it to the shell for execution and assign the result to the variable. Any newlines in the result are replaced with spaces. This is a very powerfull mechanism. You should e.g. use find instead of ls to also search in subdirectories;

SOURCES!= find . -type f -name '*.cc'

The second line uses a variable modifier to perform AT&T System V UNIX style variable substitution. It replaces the suffix .cc with the .o suffix.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
1

make on FreeBSD is different from GMake (make command on most linux systems), and those features require GMake. Run the command gmake instead.

I am not familiar with exactly which features require GMake.

Ethan McTague
  • 2,236
  • 3
  • 21
  • 53