Makefile编译

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lightjia/article/details/81299949

1.多目录编译

SUBDIRS=HdvonBase HdFramework HdThread  
RECURSIVE_MAKE=@for subdir in $(SUBDIRS);do \
	echo "making in $$subdir"; \
	(cd  $$subdir && $(MAKE)) || exit 1; \
	done  
  
RECURSIVE_CLEAN=@for subdir in  $(SUBDIRS); \
	do \
	echo "cleaning in  $$subdir";\
	(cd  $$subdir && $(MAKE) clean) || exit 1; \
	done  
  
subdirs:
	$(RECURSIVE_MAKE)  
  
all:subdirs  
  
clean:
	echo ${SUBDIRS}
	$(RECURSIVE_CLEAN)  

2.编译可执行程序

CC = g++
FLAG = -g -std=gnu++0x 
INCLUDE += -I include -I /usr/lib/libevent/include
LIBDIR = -L /usr/lib/libevent/lib
LIB = -lpthread -levent
BIN = 
TARGET = test
#OBJ_DIR = obj

DIRS = $(shell find $(shell pwd) -maxdepth 3 -type d)  
SRCS += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))  
#SRCS = $(wildcard *.cpp) 

$(TARGET):$(SRCS:.cpp=.o)
	$(CC) $(FLAG) -o $@ $^ $(LIBDIR) $(LIB)
	-rm -f *.o *.d

%.o:%.cpp
	$(CC) $(FLAG) $(INCLUDE) -c -o $@ $<

clean:
	find $(shell pwd) -name "*\.o" -o -name "*\.d" -o -name "*\.a" -o -name "*\.so"| xargs rm -rf

3.编译动静态库

LIB_NAME ?= base
CC = g++
AR	= ar
FLAG = -std=gnu++0x -fPIC
INCLUDE += -I include
LIBDIR =
LIB = -lpthread 
INSTALL_DIR = /usr/lib/mylib
DIRS = $(shell find $(shell pwd) -maxdepth 3 -type d)  
SRCS += $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))  

all:static_library shared_library
	cp -f lib${LIB_NAME}.a lib${LIB_NAME}.so ${INSTALL_DIR}/lib
	cp -f include/* ${INSTALL_DIR}/include/${LIB_NAME}

static_library:$(SRCS:.cpp=.o)
	$(AR) -cr  lib${LIB_NAME}.a $^;
	
shared_library:$(SRCS:.cpp=.o)
	$(CC) -shared -fpic -o lib${LIB_NAME}.so $^;
	
%.o:%.cpp
	$(CC) $(FLAG) $(INCLUDE) $< -c -o $@

clean:
	find $(shell pwd) -name "*\.o" -o -name "*\.d" -o -name "*\.a" -o -name "*\.so"| xargs rm -rf

猜你喜欢

转载自blog.csdn.net/lightjia/article/details/81299949