1

谢谢!,我现在更新了我的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    
4

1 回答 1

2

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

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

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

./objects/%.o: %.c

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

并且该目标的规则是错误的,它输出的./objects/$@内容将扩展到类似./objects/./objects/x86_main.o

于 2013-08-27T10:12:28.627 回答