Make file on linux - The good old question

Hi,

After googling almost every possible question on this subject I did not find any solution. I am trying build a make file which puts the object files into a separate directory. First I though this must be very easy since even the make file document has an example of it in chapter 4. Well, I was wrong. The problem is that I do not want to manually fill in the object file list so I use pattern rules so when I add another .cpp .h pair I do not have to update the make file. Sounds logical to me, maybe it isn’t since it turns out this is not a routine thing to do in the make world or at least not with short non-overcomplicated makefiles.

This works like charm however it places the .o files in the same folder as the executable and .cpp (very annoying)
CXX = g++
CXXFLAGS = -std=c++14 -g -O0 -Wall
INCL_FLAGS = -I/usr/local/include -I/usr/include/boost -I/usr/local/include/gsl
GSL_LIBS = -L/usr/local/lib
BOOST_LIBS = -L/usr/lib/x86_64-linux-gnu
CBLAS_LIBS= -L/usr/lib
LDFLAGS = -lgsl -lcblas -lboost_filesystem -lboost_system

src = $(wildcard *.cpp)
objects = $(src:.cpp=.o)

program:$(objects)
$(CXX) $(CXXFLAGS) $(INCL_FLAGS) -o $@ $^ $(GSL_LIBS) $(BOOST_LIBS) $(CBLAS_LIBS) $(LDFLAGS)

.PHONY: clean
clean:
rm -f $(objects) program

Now taking the make manual as a template I wanted run this:

CC = gcc
CXX = g++
CXXFLAGS = -std=c++14 -g -O0 -Wall
INCL_FLAGS = -I/usr/include/boost -I/usr/local/include/gsl
GSL_LIBS = -L/usr/local/lib
BOOST_LIBS = -L/usr/lib/x86_64-linux-gnu
CBLAS_LIBS= -L/usr/lib
LDFLAGS = -lgsl -lcblas -lboost_filesystem -lboost_system
OBJDIR := obj/MyMake/

src = $(wildcard *.cpp)
objects := $(addprefix $(OBJDIR),src:.cpp=.o)

all:prog

$(OBJDIR)%.o : %.cpp
$(CXX) -c $(CXXFLAGS) $< -o $@

all: $(objects) #this is line 20
$(CXX) $(CXXFLAGS) $(INCL_FLAGS) -o $@ $^ $(GSL_LIBS) $(BOOST_LIBS) $(CBLAS_LIBS) $(LDFLAGS)

$(objects): | $(OBJDIR)

$(OBJDIR):
mkdir $(OBJDIR)

.PHONY: clean
clean:
rm -f $(objects) prog

This gives an error:
Makefile2:20: *** target pattern contains no '%'. Stop.

Why does the target have to contain the literal '%' string?

I read through most of the posts on this forum but no solution found!

Thanks!

You've forgotten to say that src is a variable:
src = $(wildcard *.cpp)
objects = $(addprefix $(OBJDIR),$(src:.cpp=.o))
Last edited on
Thanks,

That was it! Works beautifully!

Topic archived. No new replies allowed.