[英]GNU Make: A better way of using both C/C++ targets with different commands
Q: Here is my Makefile fragment below: 问:这是下面的我的Makefile片段:
SRCS+=$(wildcard *.c *.cpp)
OBJECTS=$(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(SRCS:.cpp=.o)))
# .....
$(OBJ_DIR)/%.o: %.cpp
$(CXX) -ggdb -Wall -Wextra -Werror $(INC) $(C_FLAGS) -c -o $@ $<
$(OBJ_DIR)/%.o: %.c
$(CC) -ggdb -Wall -Wextra -Werror $(INC) $(C_FLAGS) -c -o $@ $<
How can I refactor expression after "OBJECTS" and remove duplication of the similar rules for .cpp and .%c? 如何在“对象”之后重构表达式并删除.cpp和。%c的类似规则的重复项?
OBJECTS := $(patsubst %,$(OBJ_DIR)/%.o,$(basename $(SRCS)))
# Note that you may not need this variable at all.
COMPILE_ARGS = -ggdb -Wall -Wextra -Werror $(INC) $(C_FLAGS) -c -o $@ $<
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(COMPILE_ARGS)
$(OBJ_DIR)/%.o: %.c
$(CC) $(COMPILE_ARGS)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.