gpt4 book ai didi

c - MakeFile - 当目标通过时避免某些文件

转载 作者:行者123 更新时间:2023-11-30 16:10:15 25 4
gpt4 key购买 nike

我正在尝试创建一个避免某些 c 文件的目标。我这样做是因为某些文件不兼容并且希望在构建过程中避免它们。波纹管是我正在使用的方法,但这样做会引起争议warning: overriding commands for target ... 。我确实了解错误的原因,但不知道更好的方法。请指导我,我是 MakeFiles 新手。提前致谢

SRCDIR = ../src
BUILDDIR = build
INCDIR = ../inc

# Compiler to use
CC = gcc

# Include paths for header files
INCLUDES = -I $(INCDIR)

# Compiler flags
# WARNING: Optimization will remove critical code (Problems seen in delay function). Use with caution.
CFLAGS = -Wall -Wextra -g $(INCLUDES) --std=gnu99
CFLAGSNOWARN = -g $(INCLUDES) --std=gnu99

# extra gcc flags used to build dependency files
DEPFLAGS = -MMD -MP

# Paths to required libraries (-L flags)
LFLAGS =

# The specific libraries that project depends on (-l flags)
LIBS = -lreadline -lpthread

# All source files
SRCS = $(wildcard $(SRCDIR)/*.c)
SRCS_1 = $(filter-out $(SRCDIR)/FileToAvoid.c, $(wildcard $(SRCDIR)/*.c))

# All object files
OBJS := $(SRCS:$(SRCDIR)/%.c=%.o)
OBJS_1 := $(SRCS_1:$(SRCDIR)/%.c=%.o)

# name of executable
MAIN = test.exe

# make all
.PHONY: all

# this is the default target
## create temporary .o files and compile main executable
all: $(MAIN)

avoidfile: $(OBJS_1)
@echo "Compiling executable: $(MAIN)"
@$(CC) $(CFLAGS) -o $(MAIN) $(OBJS_1) $(LFLAGS) $(LIBS)
@echo

$(MAIN): $(OBJS)
@echo "Compiling executable: $(MAIN)"
@$(CC) $(CFLAGS) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
@echo

# Automatically builds all object files from source files
# -c option compiles but does not link (create object files)
# -o is output filename
$(OBJS): %.o : $(SRCDIR)/%.c
@echo "Compiling object file: $@"
@$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
@echo

$(OBJS_1): %.o : $(SRCDIR)/%.c
@echo "Compiling object fileslean: $@"
@$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
@echo

最佳答案

由于要编译的两个规则相同,因此您可以制定一个规则:

$(sort $(OBJS) $(OBJS_1)): %.o : $(SRCDIR)/%.c 
echo "Compiling object file: $@"
$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@

sort 函数可以删除重复项,即使您不关心排序,也建议用于此目的。

您可能认为双冒号规则可能有帮助,但要小心!这些配方全部都被执行,导致相同源的多次编译。

关于c - MakeFile - 当目标通过时避免某些文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com