Makefile总是重建 [英] Makefile always rebuilding
问题描述
我正在使用以下简单的Makefile来构建一些简单的文件.我真的是制作Makefile的新手.我不知道为什么它会继续重建,即使文件是在首次制作之后就构建的,而且我也没有编辑任何文件.
I am using the following simple Makefile to build some simple files. I am really new to making Makefile. I don't know why it keeps in rebuilding even though the files are built after first make and I am not editing any of the file.
EXE = nextgenrsm
CC = gcc
LIBS = StarterWare_Files/
CFLAGS = -c
INCLUDE_PATH = StarterWare_Files/
MAIN_SRC = $(wildcard *.c)
MAIN_OBS = $(patsubst %.c,%.o,$(MAIN_SRC))
LIB_SRC = $(wildcard StarterWare_Files/*.c)
LIB_OBS = $(patsubst StarterWare_Files/%.c,%.o,$(LIB_SRC))
output: $(EXE)
$(EXE): $(MAIN_OBS) $(LIB_OBS)
$(CC) $(MAIN_OBS) $(LIBS)$(LIB_OBS) -o $(EXE)
$(MAIN_OBS): $(MAIN_SRC)
$(CC) $(CFLAGS) *.c -I$(INCLUDE_PATH)
$(LIB_OBS): $(LIB_SRC)
cd $(LIBS); \
$(CC) $(CFLAGS) *.c -I../
clean:
rm -rf $(LIBS)*.o $(EXE) *.o
新编辑的文件
EXE = nextgenrsm
CC = gcc
LIBS = StarterWare_Files/
CPPFLAGS = _IStarterWare_Files/
MAIN_OBS = $(patsubst %.c,%.o,$(wildcard *.c))
LIB_OBS = $(patsubst %.c,%.o,$(wildcard StarterWare_Files/*.c))
all: $(EXE)
$(EXE): $(MAIN_OBS) $(LIB_OBS)
$(CC) -o $@ $(LDFLAGS) $(MAIN_OBS) $(LIB_OBS) $(LDLIBS)
%.o: %.c
$(CC) -o $@ -MD -MP $(CPPFLAGS) $(CFLAGS) -c $^
ALL_DEPS = $(patsubst %.o,%.d,$(MAIN_OBS), $(LIB_OBS))
-include $(ALL_DEPS)
clean:
rm -f $(LIB_OBS) $(EXE) $(MAIN_OBS) $(ALL_DEPS)
.PHONY: all clean
每当我触摸StarterWare_Files/example.h并尝试再次进行制作时,都会引发错误,gcc无法使用-c或-S来指定-o或使用多个文件来指定-S.基本上,该命令变成这样的gcc -o main.o -IStarterWare_Files -c main.c StarterWare_Files/test.h StarterWare_Files/add.h ..
Whenever I do touch StarterWare_Files/example.h and try to do make again it throws me error that gcc cannot specify -o with -c or -S with Multiple files. Basically the command becomes something like this gcc -o main.o -IStarterWare_Files -c main.c StarterWare_Files/test.h StarterWare_Files/add.h..
推荐答案
您的默认目标是output
,但是您的makefile永远不会生成这样的文件,因此,每次调用make
时,它都会尝试构建output
文件
Your default target is output
but your makefile never produces such a file, so that every time you invoke make
it tries to build output
file.
解决方案是将output
目标标记为 phony目标.以及clean
目标:
The solution is to mark output
target as a phony target. As well as clean
target:
.PHONY: output clean
您可以利用内置规则从.c
和自动变量编译的.o
将您的Makefile减少为:
You can take advantage of built-in rules that compile .o
from .c
and automatic variables to reduce your makefile to:
EXE := nextgenrsm
CC := gcc
LIBS := StarterWare_Files/
CPPFLAGS := -IStarterWare_Files
MAIN_OBS := $(patsubst %.c,%.o,$(wildcard *.c))
LIB_OBS := $(patsubst %.c,%.o,$(wildcard StarterWare_Files/*.c))
all: $(EXE)
$(EXE) : $(MAIN_OBS) $(LIB_OBS)
$(CC) -o $@ $(LDFLAGS) $^ $(LDLIBS)
clean:
rm -f $(MAIN_OBS) $(LIB_OBS) $(EXE)
.PHONY: all clean
如果您想自动生成标头依赖项,请添加:
And if you would like automatic header dependency generation add:
# This rule produces .o and also .d (-MD -MP) from .c.
%.o : %.c
$(CC) -o $@ -MD -MP $(CPPFLAGS) $(CFLAGS) -c $<
# This includes all .d files produced along when building .o.
# On the first build there are not going to be any .d files.
# Hence, ignore include errors with -.
ALL_DEPS := $(patsubst %.o,%.d,$(MAIN_OBS) $(LIB_OBS))
-include $(ALL_DEPS)
clean:
rm -f $(MAIN_OBS) $(LIB_OBS) $(EXE) $(ALL_DEPS)
这篇关于Makefile总是重建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!