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

时间:2013-08-27 09:48:53

标签: 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
#-->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    

1 个答案:

答案 0 :(得分:2)

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

$(patsubst ./src/%.c,./objects/%.o,$(wildcard ./src/*.c))

您的makefile中还有其他问题,例如这个目标有一个错误的先决条件:

./objects/%.o: %.c

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

该目标的规则是错误的,它会输出到./objects/$@,这会扩展为./objects/./objects/x86_main.o