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

标签 c makefile

我希望为小型 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)

最佳答案

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

make clean

如果您创建了正确的 Makefile,所有依赖项都将自动处理,并且对于您在文件系统中所做的每项更改,您不必每次都执行“make clean”。一个简单的“make”就足够了。
如果您尚未处理 make 中的所有依赖项,那么您会注意到源代码中所做的更改不会反射(reflect)在二进制文件中。 因此,解决此问题的一个简单方法是在 Makefile 中添加这些行

clean:
    rm *.so
    rm *.o

现在,对于每个编译,执行类似的操作

make clean 
make 

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

关于c - 如何强制对每个 make 的所有文件进行完全重新编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635962/

相关文章:

c - 两个或多个命令行参数?

objective-c - 为什么 Apple 在其类中使用 "flags"结构体?

c - 在使用 makefile 的 c 程序中出现错误

c++ - 如何通过 Makefile 将 cap_net_raw 功能添加到 Linux 上的 C++ 可执行文件

c - 将 .a 库链接到 .o 对象,因此在构建时只需要包含 .o

makefile - $$i 在这个 makefile 循环中做了什么?

c - while 循环中的 scanf() 。 %前加空格不起作用

c - 如何在 C 中返回未知大小的数组

c - 如何在 Clion 中自动生成 .h 文件的函数头?

C 如何为 MPI 程序创建 Makefile?