makefile在src目录而不是objects文件夹中创建对象文件

makefile在src目录而不是objects文件夹中创建对象文件,makefile,gnu-make,Makefile,Gnu Make,谢谢!,我现在已经更新了我的makefile。和.o在src目录中创建。 这是makefile和输出。makefile抛出错误,因为所有的.o都是在src文件夹中创建的。我不知道为什么?我是新来Makefile的,所以请容忍我愚蠢的问题 # This is the Vpath; as my source directory is in the src folder - all the .c files #folder structure #Gif_Utility #-->src/*.c #

谢谢!,我现在已经更新了我的makefile。和.o在src目录中创建。 这是makefile和输出。makefile抛出错误,因为所有的.o都是在src文件夹中创建的。我不知道为什么?我是新来Makefile的,所以请容忍我愚蠢的问题

# This is the Vpath; as my source directory is in the src folder - all the .c files
#folder structure
#Gif_Utility
#-->src/*.c
#-->include/*.h
VPATH = src:include:objects
CFLAGS = -I ./include -g -Wall -DDEBUG

OBJS =./objects
# Look at the CFLAGS here; it has -DDEBUG because in my code, I have #ifdef DEBUG
# Look at the CFLAGS here; -Wall : To generate all the compiler warnings.
# include is required as my compilation depends on the .h files.

# The LD flags to link the shared objects 
#LDFLAGS= 
#in my mini-project, I am using maths library, Thus, I have lm.
# lc to link my main function with crt1.o

#what is the compiler, am I using.
#This is a good practice since I can modify these flags when cross-compiling.
cc= gcc

#PATH for the LIBS 
#This might be useful while cross-compiling.
LIBS= -lm -lc

target: $(patsubst %.c,%.o,$(wildcard ./src/*.c))
    @echo "making target"
    @mkdir -p ./objects
    $(cc) $(patsubst ./src/%.c,./objects/%.o,$(wildcard ./src/*.c)) $(LIBS) -o gif 


./objects/%.o: ./src/%.c
    @echo "making objects now"
    $(cc) $(CFLAGS) $(LDFLAGS) -c $< -o $@ 


#It is always better to write a PHONY rule for a rules like clean.
#It may happen that in source sandbox, you have a clean file. This may invoke the clean file.
#In order to prevent invoking a clean file during make clean; We give this general rule as PHONY
#PHONY tells the MAKEFILE that there is a rule clean, not a file called clean.
#Generally use PHONY for all, install, clean, distclean, 
.PHONY: clean
clean: 
    @echo "cleaning everything"
    @rm -f *.o
    @rm -f gif  
    @echo "clearning .o from src"
    @rm -f ./src/*.o
    @rm -f ./objects/*.o


$make target

cc -I ./include -g -Wall -DDEBUG   -c -o src/sysm.o src/sysm.c
cc -I ./include -g -Wall -DDEBUG   -c -o src/x86_main.o src/x86_main.c
src/x86_main.c:11:9: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
src/x86_main.c: In function ‘main’:
src/x86_main.c:16:9: warning: implicit declaration of function ‘display_init’ [-Wimplicit-function-declaration]
src/x86_main.c:19:9: warning: implicit declaration of function ‘Gif_Read’ [-Wimplicit-function-declaration]
making target
gcc ./objects/gif_display.o ./objects/gif_lzw.o ./objects/gif_read.o ./objects/sysm.o ./objects/x86_main.o -lm -lc -o gif 
gcc: error: ./objects/gif_display.o: No such file or directory
gcc: error: ./objects/gif_lzw.o: No such file or directory
gcc: error: ./objects/gif_read.o: No such file or directory
gcc: error: ./objects/sysm.o: No such file or directory
gcc: error: ./objects/x86_main.o: No such file or directory
make: *** [target] Error    
#这是Vpath;因为我的源目录在src文件夹中-所有的.c文件
#文件夹结构
#Gif_实用程序
#-->src/*.c
#-->包括/*.h
VPATH=src:include:objects
CFLAGS=-I./include-g-Wall-DDEBUG
OBJS=/对象
#看看这里的CFLAG;它有-DDEBUG,因为在我的代码中,我有#ifdef DEBUG
#看看这里的CFLAG-Wall:生成所有编译器警告。
#include是必需的,因为我的编译依赖于.h文件。
#用于链接共享对象的LD标志
#LDFLAGS=
#在我的小项目中,我正在使用数学库,因此,我有lm。
#lc将我的主要功能与crt1.o链接
#我使用的编译器是什么。
#这是一个很好的实践,因为我可以在交叉编译时修改这些标志。
cc=gcc
#LIBS的路径
#这在交叉编译时可能很有用。
LIBS=-lm-lc
目标:$(patsubst%.c、%.o、$(通配符./src/*.c))
@回声“制造目标”
@mkdir-p./objects
$(cc)$(patsubst./src/%.c、./objects/%.o、$(通配符./src/*.c))$(LIBS)-o gif
./objects/%.o:./src/%.c
@回应“现在制作对象”
$(cc)$(CFLAGS)$(LDFLAGS)-c$<-o$@
#对于像clean这样的规则,最好写一条虚假的规则。
#在源沙盒中,您可能有一个干净的文件。这可能会调用clean文件。
#为了防止在makeclean期间调用clean文件;我们认为这条一般规则是假的
#PHONY告诉MAKEFILE有一个规则clean,而不是一个名为clean的文件。
#一般使用假冒产品,安装,清洁,清洁,
.假冒:干净
清洁:
@回声“清洁一切”
@rm-f*.o
@rm-f gif
@echo“src的clearning.o”
@rm-f./src/*.o
@rm-f./objects/*.o
$make目标
cc-I./include-g-Wall-DDEBUG-c-osrc/sysm.osrc/sysm.c
cc-I./include-g-Wall-DDEBUG-c-o src/x86_main.o src/x86_main.c
src/x86_main.c:11:9:警告:“main”的第二个参数应该是“char**”[-Wmain]
src/x86_main.c:在函数“main”中:
src/x86_main.c:16:9:警告:函数'display_init'[-Wimplicit函数声明]的隐式声明
src/x86_main.c:19:9:警告:函数'Gif_Read'[-Wimplicit函数声明]的隐式声明
制定目标
gcc./objects/gif_display.o./objects/gif_lzw.o./objects/gif_read.o./objects/sysm.o./objects/x86_main.o-lm-lc-o gif
gcc:错误:./objects/gif_display.o:没有这样的文件或目录
gcc:error:./objects/gif_lzw.o:没有这样的文件或目录
gcc:错误:./objects/gif_read.o:没有这样的文件或目录
gcc:错误:./objects/sysm.o:没有这样的文件或目录
gcc:error:./objects/x86_main.o:没有这样的文件或目录
make:**[target]错误

您需要修复patsubst以更改文件名的目录部分以及后缀:

$(patsubst ./src/%.c,./objects/%.o,$(wildcard ./src/*.c))
您的makefile中还有其他问题,例如,此目标的前提条件错误:

./objects/%.o: %.c
源文件应该类似于
/src/%.c


该目标的规则是错误的,它输出到
/objects/$@
,这将扩展到类似
/objects//objects/x86\u main.o
/objects/
,而不是
/objs/
,这里有一个问题。cc-I./include-g-Wall-DDEBUG-c-o src/x86_main.o src/x86_main.c。基本上,它是在src文件夹中创建一个对象。该命令与您的任何makefile规则都不匹配,为什么当您的makefile规则将
.c
文件放在
.o
文件之前时,
.c
文件位于该文件之后?我更新了答案,指出了一些其他错误。