Makfefile笔记

Makefile编译应用程序

cc = gcc
prom = calc
deps = $(shell find ./ -name "*.h")
src = $(shell find ./ -name "*.c")
obj = $(src:%.c=%.o)       # 会将src所有的.c字串替换成.o

$(prom): $(obj)
      $(cc) -o $(prom) $(obj)

%.o: %.c $(deps)           # %.o:%.c,这是一个模式规则,表示所有的.o目标都依赖于与它同名的.c文件(当然还有deps中列出的头文件)
      $(cc) -c $< -o $@    # $<代表的是依赖关系表中的第一项(如果想引用的是整个关系表,那么就应该使用$^),具体到这里就是%.c。而$@代表的是当前语句的目标,即%.o

clean:
      rm -rf $(obj) $(prom)

Makefile编译动态链接库

CC = arm-none-linux-gnueabi-g++
SHARED = -shared -o
FPIC = -fPIC -c
RM = rm -rf

# Macro definition
DEFS = -D OS_UNIX
CFLAGS += $(DEFS)

OS_TYPE = $(shell uname -m)

ifeq ($(OS_TYPE), x86_64)
LIBS = -L./lib/x86_64 -lusb -lusb-1.0 
BIN_DIR = ./lib/x86_64/
endif 
ifeq ($(OS_TYPE), i686)
LIBS = -L./lib/x86_32 -lusb -lusb-1.0
BIN_DIR = ./lib/x86_32/
endif
ifeq ($(OS_TYPE), armv7l)
LIBS = -L./lib/Raspberry -lusb -lusb-1.0
BIN_DIR = ./lib/Raspberry/
endif

$(shell mkdir -p ${BIN_DIR})

SRC_OBJECT = $(shell find ./ -name "*.cpp")

H_OBJECT = $(shell find ./ -name "*.h")
 
OBJECT = $(src:%.c=%.o)
LIB_OBJECT = libble_sniffer.so

.PHONY:all
all:$(LIB_OBJECT)

$(LIB_OBJECT):$(OBJECT)
	$(CC) $(OBJECT) $(SHARED) -fPIC -o $(LIB_OBJECT) $(LIBS) 
	mv $(LIB_OBJECT) $(BIN_DIR)
	$(RM) -rf $(OBJS) *~ *.o

$(OBJECT):$(SRC_OBJECT) $(H_OBJECT)
	$(CC) $(CFLAGS) $(FPIC) $(SRC_OBJECT) 

clean:
	$(RM) $(BIN_DIR)

你可能感兴趣的:(Makfefile笔记)