使用gcc的c ++ makefile-从子文件夹列表生成源文件列表 [英] c++ makefile using gcc - generate source file list from a list of subfolders
问题描述
所以我有一些有效的规则:
So I have some rules that work:
# Folders
SRCDIR = src
SUBFOLDERS = test test/test2
OBJDIR = obj
# Just for me so I can read my own makefile :o
_TARGET = $@
_DEPEND = $<
# Get sources from /src and /src/test/ and /src/test/test2/
SOURCES = $(wildcard $(SRCDIR)/*.cpp ))
SOURCES = $(wildcard $(SRCDIR)/test/*.cpp ))
SOURCES = $(wildcard $(SRCDIR)/test/test2/*.cpp ))
OBJECTS = $(addprefix $(OBJDIR)/, $(patsubst src/%, %, $(SOURCES:.cpp=.o))
# Main target 'make debug'
debug: debugging $(OBJECTS)
@echo building: gcc $(OBJECTS) -o out.exe
# Compiling each file
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
g++ -c $(_DEPEND) -o $(_TARGET)
debugging:
@echo $(SOURCES)
@echo $(OBJECTS)
注意:我不得不手动复制此代码,因此可能会出现一些错误,但希望它能清楚说明我要执行的操作.
Note: I had to hand-copy this code so there may be some errors, but hopefully its clear what I am trying to do.
让我们说我想添加更多的源文件子文件夹:src/another, src/andanother
.现在,我必须添加更多的SOURCES = ...
行来执行此操作.有没有办法做到这一点.我一直在修改它,但是我还无法提出有效的规则.我想要的是在SUBFOLDERS
列表中添加一个新文件夹,然后我的代码自动提取.cpp文件.
Lets say I want to add some more source file sub folders: src/another, src/andanother
. Now I have to add more SOURCES = ...
lines to do this. Is there a way to do this in a rule. I have been tinkering with it but I can't come up with rules that work yet. What I want is that I add a new folder to SUBFOLDERS
list and my code picks up the .cpp files automatically.
注意,我不想使用诸如$(shell find ... )
之类的东西,因为Windows发现很烂,并且我希望它像possiblt Windows/Linux一样可移植.
note I don't want to use things like $(shell find ... )
because windows find sucks and I want this to be as portable as possiblt windows/Linux.
推荐答案
您可以使用make的foreach. 像:
You can use make's foreach. like:
SOURCES = $(foreach dir,${SUBFOLDERS},$(wildcard ${dir}/*.cpp))
更多信息: https://www.gnu.org/software/make/manual/html_node/Foreach-Function.html
这篇关于使用gcc的c ++ makefile-从子文件夹列表生成源文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!