2016-11-22 55 views
2

下面是我的makefile:輸出通過Makefile的一些新行

TARGET = prog 
LIBS = -lm 
CC = gcc 
CFLAGS = -pthread -Wextra -Wall -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wwrite-strings -Waggregate-return -Wcast-qual -Wswitch-default -Wswitch-enum -Wconversion -Wunreachable-code 

.PHONY: clean all default 

default: $(TARGET) clean 
all: default 

OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) 
HEADERS = $(wildcard *.h) 

%.o: %.c $(HEADERS) 
    $(CC) $(CFLAGS) -c $< -o [email protected] 

.PRECIOUS: $(TARGET) $(OBJECTS) 

$(TARGET): $(OBJECTS) 
    $(CC) $(OBJECTS) -Wall $(LIBS) -o [email protected] 

clean: 
    -rm -f *.o 

通過上面的標誌和幾個目錄中的文件,輸出使它很難找到警告:

enter image description here

如何在每次致電GCC之間獲得換行符?也許會讓警告更突出一點?

+0

像'@echo編譯$ <; @ $(CC)$(CFLAGS)-c $ <-o $ @;'? – user657267

+0

@ user657267請登錄並提出答案,以便獲得一定的功勞。這是我找到的最乾淨,最簡單的答案,謝謝。 – 8protons

回答

1

你可以沉默命令本身,並用短的東西代替它。

CPPFLAGS := -MMD -MP 
CFLAGS := -pthread -Wextra -Wall -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wwrite-strings -Waggregate-return -Wcast-qual -Wswitch-default -Wswitch-enum -Wconversion -Wunreachable-code 
LDLIBS := -lm 

objects := $(patsubst %.c, %.o, $(wildcard *.c)) 
deps := $(objects:.o=.d) 

.PHONY: all clean 

prog: $(objects) 
    $(LINK.o) $^ $(LDLIBS) -o [email protected] 

%.o: %.c 
    @echo Compiling $< 
    @$(COMPILE.c) -o [email protected] $< 

clean: ; $(RM) $(objects) $(deps) 

-include $(deps) 

一些其他的東西:

  • 不想:==
  • LDLIBS是圖書館的標準變量作爲make的使用內置的食譜
  • 使默認CCcc,這應該是你的默認編譯器的鏈接,你通常不需要設置CC
  • default目標將突破並行編譯(-j),我不會打擾它
  • 這個文件也不需要一個all規則,只要使目標的第一條規則
  • 小心與wildcard和來源,它通常是更安全的手動指定它們
  • 你讓每個目標文件依賴於每一個標題,只需使用內置depedency代代替(-MMD -MPinclude
  • 化妝已經爲對象鏈接和編制食譜,重用他們
  • 使默認$(RM)rm -f
+0

快速添加:可以使用'$(Q)',而不是在$(COMPILE.c)之前放置@,而$(Q)'通常會解析爲'@'或者如果$(V)'設置爲1,則爲空。這樣,如果您正在調試,則可以鍵入'make V = 1'來查看完整的命令。 – user2766918