makefile:子目录的模式规则

时间:2017-04-04 23:20:17

标签: c++ makefile pattern-matching

这是我的项目:

project
  |--- main.cpp
  |--- makefile
  |--- test
        |--- Test.cpp
        |--- Test.h

以下是makefile

g++1x:=g++ -std=c++14 -stdlib=libc++ -MMD -MP
cflags:= -Wall -lncurses

PATHS:=./ ./test/
TARGET:=matrix.out
SRC:=$(foreach PATH,$(PATHS),$(wildcard $(PATH)/*.cpp))
OBJDIR:=.obj
OBJ:=$(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.o)))


.PHONY: install
install: $(OBJDIR) $(TARGET)

$(OBJDIR):
    mkdir -p $(OBJDIR)

$(TARGET): $(OBJ)
    $(g++1x) $(cflags) -o $@ $^ -g


$(OBJDIR)/%.o: %.cpp
    $(g++1x) -c -o $@ $< -g

$(OBJDIR)/%.o: ./test/%.cpp
    $(g++1x) -c -o $@ $< -g

-include $(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.d)))

.PHONY: clean
clean:
    rm -f $(TARGET)
    rm -rf $(OBJDIR)

效果很好,但我有两个问题:
1)是否可以避免foreach PATHS,以便我可以对所有cpp项目使用相同的makefile
2)如你所见,要生成main.oTest.o,我会写两个块:
$(OBJDIR)/%.o: ./test/%.cpp$(OBJDIR)/%.o: %.cpp
是否可以只写一次?
我尝试过如下但不起作用:

$(OBJDIR)/%.o: $(foreach PATH,$(PATHS),$(wildcard $(PATH)/%.cpp))
        $(g++1x) -c -o $@ $< -g

我甚至尝试过这样但是它仍然不起作用:

$(OBJDIR)/%.o: %.cpp ./test/%.cpp
    $(g++1x) -c -o $@ $< -g

1 个答案:

答案 0 :(得分:7)

您应该将源树保留在对象树中。通过这种方式,可以更轻松地创建全局规则并保持依赖关系。

  PARENT_MOL_CHEMBL_ID ABL EGFR TP53
    C10                 1    1    0
   C939                 0    0    1
'return' outside function

编辑:您可以将对象目录添加为order only prerequisites,将# Make the request to publish and check status code print("\tUploading...") server_response = requests.post(publish_url, data=payload, headers={'x-tableau-auth': auth_token, 'content-type': content_type}) _check_status(server_response, 201) return 的数量减少到最小值:

# Use the shell find command to get the source tree
SOURCES := $(shell find * -type f -name "*.c")

OBJDIR  := .objects

# Keep the source tree into the objects tree
OBJECTS := $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o))

all: mytarget

mytarget: $(OBJECTS)
    $(CC) $^ -o $@

# As we keep the source tree we have to create the
# needed directories for every object
$(OBJECTS): $(OBJDIR)/%.o: %.c
    mkdir -p $(@D)
    $(CC) -MMD -MP -c $< -o $@

-include $(OBJECTS:.o=.d)