生成文件中的 $< 似乎给出空输出

$< in makefile seems to give empty output

我正在尝试为我的测试 C++ 项目编写一个 makefile,该项目使用单独的源代码和目标文件目录。 我在 Whosebug 上关注了另一个问题的答案(遗憾的是我再也找不到了),但我一定是做错了什么。

我的 makefile 如下所示:

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

这里是项目目录:

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

2 directories, 5 files

当我尝试 运行 make 时,我得到了这个:

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

g++ -I /usr/include/boost/ -c -o src/message.o 的行来看,$< 似乎给了我一个空输出。这是为什么?

根据评论,OBJECTS 中的所有目标文件路径仍然具有作业中的 src/ 前缀...

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

相反,您需要使用(未测试)替换目录前缀和扩展名...

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