Makefile文件 - 编译到不同的目标文件夹 [英] Makefiles - Compiling to a different target folder

查看:922
本文介绍了Makefile文件 - 编译到不同的目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有库文件夹是我指定一个make文件的列表:

I have a list of library folders that I'm specifying in a make file:

LIBRARIES = Ethernet                \
            SPI                     \
            SD

我也有一些规则为每一个编译到我的项目的本地文件夹的obj:

I also have a number of rules for compiling each one into a local obj folder of my project:

obj/SPI/%.cpp.o: $(LIBS_DIR)/SPI/%.cpp
    @echo "Compiling $<"

obj/SD/%.cpp.o: $(LIBS_DIR)/SD/%.cpp
    @echo "Compiling $<"    

obj/Ethernet/%.cpp.o: $(LIBS_DIR)/Ethernet/%.cpp
    @echo "Compiling $<"    

(注意,每一个使用它自己的子文件夹)。我怎么能生成$这些规则(库)自动,这样我就不必重复它们这样?

(Note that each one uses it's own sub-folder). How can I generate these rules from $(LIBRARIES) automatically so that I don't have to duplicate them like this?

推荐答案

您可以这样做:

obj/%.cpp.o : $(LIBS_DIR)/%.cpp
    g++ -c -o $@ ${CPPFLAGS} ${CXXFLAGS} $<

在上面捕获 DIR / src目录部分。

如果您想自动创建它可以用的​​继发性扩张使功能:

If you'd like the output directories to be created automatically it can be done with secondary expansion make feature:

.SECONDEXPANSION:

obj/%.cpp.o : $(LIBS_DIR)/%.cpp | $${@D} # object file depends on its directory
    g++ -c -o $@ ${CPPFLAGS} ${CXXFLAGS} $<

obj/% : # directory creation rule
    mkdir -p $@

这篇关于Makefile文件 - 编译到不同的目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆