makefile多个带有通配符的main

时间:2017-09-05 14:54:59

标签: makefile wildcard

我想编译在包含许多主电源的文件夹中自动找到的许多电源。

SOURCES       = $(wildcard src/shared/*.cc)
OBJECTS       = $(SOURCES:%.cc/%.o)
MAINS_SOURCES = $(wildcard src/mains/*.cc)
MAINS_OBJECTS = $(MAINS_SOURCES:%.cc=%.o)
PROGRAMS_NAME = $(MAINS_SOURCES:src/mains/%.cc=%)

现在我有这个代码当然不能用于多个主程序。

$(PROGRAMS_NAME):$(OBJECTS) $(MAINS_OBJECTS)
    $(CXX) $(LIBS) $(OBJECTS) $(MAINS_OBJECTS) -o $@

我如何正确地为每个主要分割$(MAINS_OBJECTS)

谢谢!

1 个答案:

答案 0 :(得分:1)

对于这种情况,static pattern rules通常很方便:

SOURCES       = $(wildcard src/shared/*.cc)
OBJECTS       = $(SOURCES:%.cc/%.o)
MAINS_SOURCES = $(wildcard src/mains/*.cc)
MAINS_OBJECTS = $(MAINS_SOURCES:%.cc=%.o)
PROGRAMS_NAME = $(MAINS_SOURCES:src/mains/%.cc=%)

$(OBJECTS) $(MAINS_OBJECTS): %.o: %.cc
    $(CXX) $(CXXFLAGS) -c $< -o $@

$(PROGRAM_NAME): %: src/mains/%.o $(OBJECTS)
    $(CXX) $(LIBS) $^ -o $@