场景:有a.cpp b.cpp c.cpp….共计n个源文件,每个源文件要生成一个对应的名为a b c …的可执行文件。

Makefile示例:

CXX = g++
LIB = ../lib/source/libDs.a
INCLUDE = ../lib/include/
CFLAGS = -I$(INCLUDE)

bins =		a \
		b \
		c

all :
	for i in $(bins); do \
		$(CXX) -c $(CFLAGS) $(addsuffix .cpp, $$i) -o $(addsuffix .o, $$i); \
		$(CXX) -o $$i $(addsuffix .o, $$i) $(LIB); \
	done

clean : 
	for i in $(bins); do \
		rm -f $(addsuffix .o, $$i); \
		rm -f $$i; \
	done

主要就是利用addsuffix来处理这种有规律的匹配,再用for语句加以循环。以后每增加一个源文件,就在bins那里加一行即可。

相关文章:

  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案