San Jacobs
San Jacobs

Reputation: 47

$< in makefile seems to give empty output

I'm trying to write a makefile for my test C++ project that works with separate directories for source code and object files. I followed an answer to another question on StackOverflow (which I sadly can't find anymore), but I must've done something wrong.

My makefile looks like this:

INCLUDE=-I /usr/include/boost/

LIBDIR=-L /usr/lib/x86_64-linux-gnu/

LIBS=-lboost_date_time

SOURCES=$(wildcard src/*.cpp)
OBJDIR=obj/
OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES))

a.out: $(OBJECTS)
    g++ $(LIBDIR) $(LIBS) $(OBJECTS)

$(OBJECTS): obj/%.o : src/%.cpp
    g++ $(INCLUDE) -c $< -o $@

clean:
    rm obj/*.o

cleanall:
    rm obj/*.o a.out

And here is the project directory:

~/Programming/testing-cpp 
❯ tree
.
├── makefile
├── obj/
└── src/
    ├── main.cpp
    ├── message.cpp
    └── message.h

2 directories, 5 files

When i try to run make, I get this:

~/Programming/testing-cpp 
❯ make
makefile:25: target 'src/message.o' doesn't match the target pattern
makefile:25: target 'src/main.o' doesn't match the target pattern
g++ -I /usr/include/boost/ -c  -o src/message.o
g++: fatal error: no input files
compilation terminated.
make: *** [makefile:26: src/message.o] Error 1

From the line that reads g++ -I /usr/include/boost/ -c -o src/message.o it seems that $< is giving me an empty output. Why is this?

Upvotes: 0

Views: 227

Answers (1)

G.M.
G.M.

Reputation: 12889

As per the comment, all of your object file paths in OBJECTS still have the src/ prefix from the assignment...

OBJECTS=$(patsubst %.cpp, %.o, $(SOURCES))

Instead you need to replace both the directory prefix and the extension using (untested)...

OBJECTS=$(patsubst src/%.cpp, $(OBJDIR)%.o, $(SOURCES))

Upvotes: 1

Related Questions