天天看點

Makefile編譯多個可執行檔案

簡單執行個體:

CC = gcc
CXX = g++
CFLAGS = -O -DDBG -D_REENTRANT -Wall -DUSE_PTHREAD -pthread
LDFLAGS = -ldl -lnsl -lm -lpthread

SRCS = $(wildcard *.cpp)
OBJS = $(patsubst %cpp, %o, $(SRCS))  //利用模式比對生成.o檔案

DATALIBS = dblib/lib.a
ZIPLIBS = ZipCoder/ZipCoder.a  

TARGET = ../bin/excute test
all: $(TARGET)

../bin/excute : excute.o $(filter-out test.o,$(OBJS)) //消除生成多個可執行檔案的影響,如果有更多個:$(filter-out $(test1.o,test2.o..),$(OBJS))
		$(CXX) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(ZIPLIBS) $(DATALIBS)
test :  test.o $(filter-out excute.o,$(OBJS))
		$(CXX) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(ZIPLIBS) $(DATALIBS)

#$(ZIPLIBS): ZipCoder/Makefile ZipCoder/zlib/Makefile
#	cd ZipCoder/zlib/; make; cd ..; make; cd ..;

%.o : %.c
	$(CC) $(CFLAGS) -c $^ -o $@
%.o : %.cpp
	$(CXX) $(CFLAGS) -c $^ -o $@	

.PHONY : clean

clean :
	rm -f *.o
	rm -f ../bin/excute

# install:
# 		mv Excute excute; cp -f ordermisd ../bin/;


           

一個makefile學習位址:

http://www.cnblogs.com/wang_yb/p/3990952.html