$<在 makefile 中似乎给出了空输出

发布于 2025-01-10 05:09:29 字数 1206 浏览 0 评论 0 原文

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

我的 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 似乎 $< 给了我一个空输出。这是为什么呢?

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 
lt; -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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2025-01-17 05:09:29

根据评论,OBJECTS 中的所有对象文件路径仍然具有分配中的 src/ 前缀...

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

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

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文