Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

$< 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

~/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?

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading