如何强制对每个 make 的所有文件进行完全重新编译?

How to force a full recompilation of all files for every make?

我希望为小型 C 应用程序创建一个基本的 makefile 模板。在这种情况下,我不太关心性能而不是清晰度,并且想重新编译所有内容 - 所有 .h 和 .c 文件,以及第三方 .so 文件。

# Constants
#===============================================================================

# Specify the C complier
CC = gcc

# List the flags to pass to the compiler
#  -ggdb       Compile with debug information for gdb
#  -Wall       Give all diagnostic warnings
#  -O0         Do NOT optimize generated code
#  -m64        Generate code for a 64-bit environment
CFLAGS = -ggdb -Wall -O0 -m64

# Change the list of c source files into a list of object files by replacing
# the .c suffix with .o
OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))

# List libraries
# m    Math library
LIBRARIES = -lm

# Specify the build target
TARGET = heyyou

# Rules
#===============================================================================
# $@ = left side of the colon, the target
$(TARGET) : $(OBJECTS)
    @echo "Compiling $@..."
    $(CC) -o $(TARGET) $(OBJECTS) $(CFLAGS) $(LIBRARIES)

如果你已经正确地考虑了所有的礼仪,只需使用

make clean

如果您制作了适当的 Makefile,所有依赖项都会自动处理,并且对于您在文件系统中所做的每个更改,您不必每次都执行 "make clean"。一个简单的 "make" 就足够了。
如果您没有处理 make 中的所有依赖项,那么您会注意到在源代码中所做的更改不会反映在二进制文件中。 因此,一个简单的解决方法是在 Makefile

中添加这些行
clean:
    rm *.so
    rm *.o

现在,对于每个编译,做类似

的事情
make clean 
make 

这不是处理 Makefile 的正确方法,但在某些令人沮丧的情况下它是一个救星。

您可以在 makefile 中添加一个干净的函数,例如:

clean:
    rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak
    rm -rf *.lst *.map

对于您的情况,您还可以添加:

rm -rf $(OBJECTS) 

那么你只需在调用 make

之前调用 make clean

您可以在 make 命令行中指定 -B 选项。

make -B