GNU Make模式以在与src不同的目录中构建输出 [英] GNU Make pattern to build output in different directory than src
本文介绍了GNU Make模式以在与src不同的目录中构建输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个Makefile,将我的.o
文件放置在与源文件不同的目录中.我正在尝试使用模式规则,因此不必为每个来源&目标文件.
I'm trying to create a Makefile which places my .o
files in a different directory than my source files. I'm trying to use a pattern rule so I don't have to create identical rules for each source & object file.
我的项目结构如下:
project/
+ Makefile
+ src/
+ main.cpp
+ video.cpp
+ Debug/
+ src/ [contents built via Makefile:]
+ main.o
+ video.o
我的Makefile类似于:
My Makefile looks something like:
OBJDIR_DEBUG = Debug
OBJ_DEBUG = $(OBJDIR_DEBUG)/src/main.o $(OBJDIR_DEBUG)/src/video.o
all: $(OBJ_DEBUG)
$(OBJ_DEBUG): %.o: %.cpp
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@
这不起作用,因为它在Debug/src/*.cpp
处查找我的源文件.
This doesn't work, because it looks for my source files at Debug/src/*.cpp
.
我尝试了以下操作:
# Broken: make: *** No rule to make target `Debug/src/main.cpp', needed by `Debug/src/main.o'. Stop.
# As a test, works if I change "%.cpp" to "Debug/src/main.cpp", though it obv. builds the wrong thing
# Strip OBJDIR_DEBUG from the start of source files
$(OBJ_DEBUG): %.o: $(patsubst $(OBJDIR_DEBUG)/%,%,%.cpp)
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@
# Broken:
# Makefile:70: target `src/main.o' doesn't match the target pattern
# Makefile:70: target `src/video.o' doesn't match the target pattern
# Add OBJDIR_DEBUG in target rule
OBJ = src/main.o src/video.o
$(OBJ): $(OBJDIR_DEBUG)/%.o: %.cpp
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@
推荐答案
After re-reading the documentation on static pattern rules, I derived the following pattern rule which seems to work.
$(OBJ_DEBUG): $(OBJDIR_DEBUG)/%.o: %.cpp
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@
我不确定这是否是最好的方法,我愿意提出建议.
I'm not sure this is the best approach, and I'm open to suggestions.
这篇关于GNU Make模式以在与src不同的目录中构建输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文