我正在尝试学习制作文件。我的目录结构是$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' . |-bin |---exe |---obj |-build |-include |-lib |-make |-source我想做的是将包含文件conversion.h放在include文件夹中,将all .c files放在source中,将makefile放在make中,将已编译的all .o files放在bin/obj中,并将exe放在我提到以下帖子:makefile include *.h file in other directoryUsing make to move .o files to a separate directory我的makefile是:VPATH= ./../sourceOBJDIR= ./../bin/objEXEDIR= ./../bin/exe#vpath %.o $(OBJDIR)CFLAGS= -Wall -c -I.#INCLUDES= -I./../includeobjects= binary.o hex.o octal.oconversion: $(objects)# gcc -Wall -o conversion $(objects) -I.binary.o: binary.c conversion.h gcc $(CFLAGS) $< -o $(OBJDIR)/[email protected]: octal.c conversion.h gcc $(CFLAGS) $< -o $(OBJDIR)/[email protected]: hex.c conversion.h gcc $(CFLAGS) $< -o $(OBJDIR)/$@clean: rm -rf $(OBJDIR)/*.o *.o *~ conversion我正在使用cygwin。我的问题是:1)我无法从位置/bin/exe包含我的conversion.h如果将./../include复制到-I.文件夹,conversion.h可以正常工作-但只要我替换为make,而在-I./../include文件夹中没有任何conversion.h副本,我低于错误$ makemake: *** No rule to make target 'conversion.h', needed by 'binary.o'. Stop.2)我的makefile确实将所有make文件都放置到.o,但是当我尝试使用如下所示的/bin/obj时(而不是使用像-vpath这样的手动放置)vpath %.o $(OBJDIR)... $(OBJDIR)/binary.o: binary.c conversion.h gcc $(CFLAGS) $< -o $@......执行以上所有.o规则的替换,不将-o $(OBJDIR)/$@放置到all .o files目录任何帮助,将不胜感激。谢谢 最佳答案 在定义目标及其依赖项时,必须明确说明.h文件,.c文件,.o文件和可执行文件的位置。VPATH= ./../sourceINCLUDEDIR= ./../includeOBJDIR= ./../bin/objEXEDIR= ./../bin/exe#vpath %.o $(OBJDIR)INCLUDES= -I./../includeCFLAGS= -Wall -c -I. $(INCLUDES)objects= $(OBJDIR)/binary.o $(OBJDIR)/hex.o $(OBJDIR)/octal.o$(EXEDIR)/conversion: $(objects)# gcc -Wall -o conversion $(objects) -I.$(OBJDIR)/binary.o: $(VPATH)/binary.c $(INCLUDEDIR)/conversion.h gcc $(CFLAGS) $< -o $@$(OBJDIR)/octal.o: $(VPATH)/octal.c $(INCLUDEDIR)/conversion.h gcc $(CFLAGS) $< -o $@$(OBJDIR)/hex.o: $(VPATH)/hex.c $(INCLUDEDIR)/conversion.h gcc $(CFLAGS) $< -o $@clean: rm -rf $(OBJDIR)/*.o *.o *~ $(EXEDIR)/conversion关于c - 包括.h文件并将.o文件定向到目录:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23177853/ 10-13 09:31