假设有a.c,b.c,c.c,tt/at.c,tt/bt.c,fff/af.c,fff/bf.c.所以我像这样制作Makefile:
Suppose there are a.c, b.c, c.c, tt/at.c, tt/bt.c, fff/af.c, fff/bf.c. So I make Makefile like this:
OBJS=a.o b.o c.o SRCS=$(OBJS:.o=.cc) OBJS_TT=at.o bt.o SRCS_TT=tt/at.c tt/bt.c OBJS_FFF=af.o bf.o SRCS_FFF=fff/af.c fff/bf.c TARGET=test .cc.o: gcc -c $< $(OBJS_TT): gcc -c $(SRCS_TT) $(OBJS_FFF): gcc -c $(SRCS_FFF) all: gcc -o $(TARGET) $(OBJS) $(OBJS_TT) $(OBJS_FFF)如果添加了tt目录中的C文件,则必须在SRCS_TT&中添加文件名. OBJS_TT. 有没有一种方法可以改善make文件? 如何使用后缀规则处理所有c文件(包括目录中的文件)?
If C files in tt directory are added, I have to add file name in SRCS_TT & OBJS_TT. Is there a way to improve make file? How do I do all c files (including files in directory) using suffix rules ?
推荐答案当源文件位于不同目录中时,一种常见的解决方案是在build目录中构建同构目录结构.这样,您可以拥有多个具有相同文件名(例如util.cc)但位于不同目录中的源,并且目标文件不会冲突,因为它们位于不同的目录中.
One common solution when source files reside in different directories is to build an isomorphic directory structure in the build directory. This way you can have multiple sources with the same file name (e.g. util.cc) but in different directories and the object files won't collide because they get built into different directories.
一个使用同构构建目录结构的工作示例,该结构也具有自动生成的标头依赖项:
A working example that creates an isomorphic build directory structure also with auto-generated header dependencies:
build_dir := build all : ${build_dir}/test .SECONDEXPANSION: .SECONDARY: test_srcs := a.cc b.cc x/c.cc y/d.cc ${build_dir}/test : ${test_srcs:%.cc=${build_dir}/%.o} | ${build_dir}/ g++ -o $@ ${LDFLAGS} ${LDLIBS} $^ # Include the auto-generated dependencies. -include ${test_srcs:%.cc=${build_dir}/%.d} # Compile and generate dependency files. ${build_dir}/%.o : %.cc | $$(dir $$@) g++ -o $@ -c -MD -MP ${CPPFLAGS} ${CXXFLAGS} $< # Directory build rules. while loop to handle races on mkdir during parallel builds. ${build_dir}/%/ : | ${build_dir}/ while ! mkdir -p $@; do true; done # Build root directory rule. ${build_dir}/ : mkdir -p $@ # Don't try to rebuild these, these are generated when compiling. ${build_dir}/%.d : ; clean : rm -rf ${build_dir} .PHONY : all clean用法示例:
$ mkdir x y $ touch b.cc x/c.cc y/d.cc $ echo "int main() {}" > a.cc $ make mkdir -p build/ g++ -o build/a.o -c -MD -MP a.cc g++ -o build/b.o -c -MD -MP b.cc while ! mkdir -p build/x/; do true; done g++ -o build/x/c.o -c -MD -MP x/c.cc while ! mkdir -p build/y/; do true; done g++ -o build/y/d.o -c -MD -MP y/d.cc g++ -o build/test build/a.o build/b.o build/x/c.o build/y/d.o $ tree build/ build/ ├── a.d ├── a.o ├── b.d ├── b.o ├── test ├── x │ ├── c.d │ └── c.o └── y ├── d.d └── d.o 2 directories, 9 files $ make make: Nothing to be done for 'all'. $ make clean rm -rf build更多推荐
在Makefile中使用后缀规则
发布评论