只在Makefile实际完成时打印某些内容

huangapple go评论51阅读模式
英文:

Print something in Makefile only if it was actually done

问题

我正在尝试使我的Makefile输出文本而不是它正在执行的命令,并且我发现在命令之前加上“@”将会使它们静音,我可以使用 echo 来显示我想要的内容。
然而,当我的项目完全构建并运行 make 时,它会输出 make[1]: Nothing to be done for all'` 以及我的文本,并且我想知道是否有办法只在Makefile实际执行操作时运行 echo 命令(或其他命令)。
这是我的当前的Makefile,你可以看到我到目前为止做了什么:

NAME = push_swap

SRCS = push_swap.c push.c swap.c rotate.c reverse.c \
		pick_sort.c push_swap_min_max.c small_stack.c big_stack.c \
		repeat_functions.c
OBJS = $(SRCS:.c=.o)

LIBFT = libft

CC = gcc
CFLAGS = -Wall -Werror -Wextra
MAKEFLAGS = --no-print-directory

RM = rm -f

all: $(NAME)

$(NAME): $(LIBFT) $(OBJS)
	@$(CC) $(CFLAGS) $(OBJS) ./libft/libft.a -o $(NAME)
	@echo "链接成功"

$(OBJS):
	@$(CC) $(CFLAGS) -c $(SRCS)
	@echo "编译成功"

$(LIBFT):
	@if [ ! -d "libft" ]; then git clone https://github.com/Nixo371/libft-42; echo "克隆了libft存储库"; fi
	@$(MAKE) -C libft all
	@echo "libft编译完成"

clean:
	@$(RM) $(OBJS)
	@$(MAKE) -C libft clean
	@echo "从push_swap中删除了对象文件"

fclean: clean
	@$(RM) $(NAME)
	@$(MAKE) -C libft fclean
	@echo "删除了 $(NAME)"

re: fclean all

.PHONY: all libft clean fclean re

我已经尝试查看文档,看是否能找到 make 的标签/选项,以便静音命令,或者使用其退出码(我假设 make[1]: Nothing to be done for 'all' 中的1是退出码)来提供不同的结果。但我没有找到这样的内容。

英文:

I'm trying to make my Makefile output text instead of the commands it is executing, and I found preceding commands with "@" will mute them, and I can use echo to display whatever I want.
However, when my project is fully built and I run make, it will output make[1]: Nothing to be done for all'.` as well as my text, and I was wondering if there was a way for me to only run echo commands (or whatever) when the Makefile actually does something.
This is my current Makefile, so you can see what I have so far:

NAME = push_swap

SRCS = push_swap.c push.c swap.c rotate.c reverse.c \
		pick_sort.c push_swap_min_max.c small_stack.c big_stack.c \
		repeat_functions.c
OBJS = $(SRCS:.c=.o)

LIBFT = libft

CC = gcc
CFLAGS = -Wall -Werror -Wextra
MAKEFLAGS = --no-print-directory

RM = rm -f

all: $(NAME)

$(NAME): $(LIBFT) $(OBJS)
	@$(CC) $(CFLAGS) $(OBJS) ./libft/libft.a -o $(NAME)
	@echo "linked successfully"

$(OBJS):
	@$(CC) $(CFLAGS) -c $(SRCS)
	@echo "compiled successfully"

$(LIBFT):
	@if [ ! -d "libft" ]; then git clone https://github.com/Nixo371/libft-42; echo "cloned libft repository"; fi
	@$(MAKE) -C libft all
	@echo "libft compiled correctly"


clean:
	@$(RM) $(OBJS)
	@$(MAKE) -C libft clean
	@echo "removed object files from push_swap"

fclean: clean
	@$(RM) $(NAME)
	@$(MAKE) -C libft fclean
	@echo "removed $(NAME)"

re: fclean all

.PHONY: all libft clean fclean re

I have tried looking in the documentation to see if I could find tags/options for make that would mute the command, or use its exit code (I'm assuming the 1 in make[1]: Nothing to be done for all'.` is the exit code) to provide different results. But I couldn't find anything of the sort

答案1

得分: 1

The make[1] 笔记是来自对 make 的递归调用的消息 — 中的 1 表示它在递归中处于一级深度。

在这种情况下,它很可能来自于你对 LIBFT 执行的操作:

@$(MAKE) -C libft all

这将使用 libft 目录中的 Makefile 为目标 all 运行 make(显然与该目标无关)。

你可以通过将其 stdout/stderr 重定向到 /dev/null 来完全关闭它:

@$(MAKE) -C libft all >/dev/null 2>&1

或者,你可以尝试修改该子目录的 Makefile,使其在没有工作要做时保持静默。

英文:

The make[1] note is a message coming from a recursive invocation of make -- the 1 meaning it is one level deep in the recursion.

In this case it is likely coming from the action you have for LIBFT:

@$(MAKE) -C libft all

which will run make using the Makefile in the libft directory for the target all (and which apparently has nothing to do for that target)

You can shut that up completely by redirecting its stdout/stderr to /dev/null:

@$(MAKE) -C libft all >/dev/null 2>&1

Or you may be able to change that subdirectory Makefile to be silent when there is nothing to do.

huangapple
  • 本文由 发表于 2023年2月24日 03:14:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定