這個makefile的行爲並不像我期望的那樣。我希望它爲當前目錄和子目錄中的每個.c文件構建.o文件,並將它們放入一個靜態庫中。但是,它會在第一個或第二個文件後停止應用我的$(INCS)。當它試圖構建第二個.o文件時,我在構建行中看不到-I路徑,並且抱怨沒有在其中找到頭文件。名稱已被泛化以簡化事物。我在Windows XP上使用cygwin。我正在使用不在cygwin樹下的ARM交叉編譯器。我基於這個生成文件的回答here。只有大約二十個.c文件,因此以這種方式創建依賴文件的開銷並不大。爲什麼這個makefile不適用於所有對象?
# Project specific options
CC = my-cross-gcc
INCS := -I. -Iinc
INCS += -Imy/inc/path
CFLAGS := -Wall -fPIC -static -cross-compiler-specific-options
OUT := bin/libmylib.a
MKDIR:=mkdir -p
### Generic C makefile items below:
# Add .d to Make's recognized suffixes.
SUFFIXES += .d
NODEPS:=clean
#Find all the C files in this directory, recursively
SOURCES:=$(shell find . -name "*.c")
#These are the dependency files
DEPFILES:=$(patsubst %.c,%.d,$(SOURCES))
OBJS:= $(patsubst %.c,%.o,$(SOURCES))
#Don't create dependencies when we're cleaning, for instance
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
-include $(DEPFILES)
endif
#This is the rule for creating the dependency files
%.d: %.c
$(CC) $(INCS) $(CFLAGS) -MM -MT '$(patsubst %.c, %.o,$(patsubst %.c,%.o,$<))' $< > [email protected]
#This rule does the compilation
%.o: %.c %.d %.h
$(CC) $(INCS) $(CFLAGS) -o [email protected] -c $<
# Now create a static library
all: $(OBJS)
@$(MKDIR) bin
ar rcsvq $(OUT) $(OBJS)
clean:
rm -rf $(OBJS) $(OUT) $(DEPFILES)
爲什麼這個Makefile 不申請$(INCS)建立後續.o文件時?我如何解決它?輸出類似於此:
$ make all
my-cross-gcc -I. -Iinc -Imy/inc/path -<compiler options> -o firstfile.o -c firstfile.c
my-cross-gcc -I. -Iinc -Imy/inc/path -<compiler options> -o secondfile.o -c secondfile.c
my-cross-gcc -<compiler flags> -o thirdfile.o -c thirdfile.c
thirdfile.c:23:18: fatal error: myinc.h: No such file or directory
compilation terminated.
當我去海灣合作委員會一線構築thirdfile.o和使用-I路徑的命令行和類型,目標文件被成功建立。
是否有'firstfile.h'和'secondfile.h'但沒有'thirdfile.h'? –
與你的問題沒有關係,但你似乎沒有在任何地方包括依賴文件,使它們無用。 – interjay
interjay,我似乎已經簡化了我輸入的makefile。我會在上面添加這些行。 – jasper77