Makefile 分析入门

从一个例子分析Makefile的一些用法

HOME_PATH := ../../../..
DESTINATION := GxIAPITest
LIBS := gtest gtest_main pthread gxiapi

CURRENT_PATH := .

INC_PATH := ${HOME_PATH}/inc

INCLUDES += ${HOME_PATH} ${HOME_PATH}/src/jni/GxIAPI ${HOME_PATH}/src/jni/TLClass ${HOME_PATH}/inc/Genicam ${INC_PATH}
INCLUDES_FLAG := $(foreach n, ${INCLUDES}, -I$(n))

LINK_PATH := ../../lib
LINK_FLAG := $(foreach n, ${LINK_PATH}, -L$(n))


MY_FILES_SUFFIX_CPP := %.cpp
MY_ALL_FILES := $(shell find $(CURRENT_PATH) -name "*.*")
SOURCE_CPP  := $(filter $(MY_FILES_SUFFIX_CPP),$(MY_ALL_FILES))

all: ${DESTINATION}


$(DESTINATION) : 
	g++ $(INCLUDES_FLAG) -o $@ $(SOURCE_CPP) $(LINK_FLAG) $(addprefix -l,$(LIBS)) $(CPPFLAGS) -Wl,-rpath=.
	

规则解释

  1. HOME_PATH := 这些是声名变量的。
  2. += 表明追加变量
  3. INCLUDES_FLAG := $(foreach n, I N C L U D E S , − I {INCLUDES}, -I INCLUDES,I(n)) 表明将INCLUDES中每一个元素都设置成-l
  4. ( a d d p r e f i x − l , (addprefix -l, (addprefixl,(LIBS)) 表明将没一个库都-l一下。
  5. 过滤出本地目录下的cpp文件。
MY_FILES_SUFFIX_CPP := %.cpp
MY_ALL_FILES := $(shell find $(CURRENT_PATH) -name "*.*")
SOURCE_CPP  := $(filter $(MY_FILES_SUFFIX_CPP),$(MY_ALL_FILES))
  1. -Wl,-rpath 表明运行库的查找目录。

可能遇到的问题

。。。

较复杂一点的

HOME_PATH := ../..

DESTINATION := ./libgxiapi.so
LIBS := pthread dl rt
INC_PATH := $(HOME_PATH)/../../inc

RM := rm -f
CP := cp -f
#C,CC或CPP文件的后缀
PS=cpp

PS1=xml
# GNU Make的隐含变量定义
CC = g++-4.8
#ar
AR = ar
#HOME_INCLUDE := $(shell find $(HOME_PATH) -maxdepth 6 -type d)
HOME_INCLUDE := $(HOME_PATH) $(HOME_PATH)/xxx $(HOME_PATH)/xxx $(HOME_PATH)/xxx/xxx/xxx/ $(HOME_PATH)/xxx  
HOME_INCLUDE += $(HOME_PATH)/xxx $(HOME_PATH)/xxx 
HOME_INCLUDE_FLAG := $(foreach n,$(HOME_INCLUDE),-I$(n))

INC_INCLUDE := $(INC_PATH) $(INC_PATH)/xxx $(INC_PATH)/xxx/xxx/c/xxx
INC_INCLUDE_FLAG := $(foreach n,$(INC_INCLUDE),-I$(n))

INCLUDES = -I../jni/
INCLUDES += $(HOME_INCLUDE_FLAG)
INCLUDES += $(INC_INCLUDE_FLAG)

# CPPFLAGS = -ggdb -Wall 
CPPFLAGS += -O2
CPPFLAGS += -m32 -I/usr/include/x86_64-linux-gnu/
CPPFLAGS += -w -fpermissive -Wno-deprecated -fPIC -shared --std=c++11

CPPFLAGS_INC += $(INCLUDES)
CPPFLAGS += -DHAVE_CONFIG_H

#expat Macro
CPPFLAGS += -DHAVE_UNISTD_H
CPPFLAGS += -DXML_DEV_URANDOM

CPPFLAGS += -D GENICAM_COMPANY_SUFFIX=GALAXY -DGCBASE_EXPORTS


CPPFLAGS_HIDDEN += -fvisibility=hidden

MY_FILES_SUFFIX_CPP := %.cpp 
MY_FILES_SUFFIX_C   := %.c
MY_FILES_SUFFIX_CXX := %.cxx

ignore_dir := -path "$(HOME_PATH)/xxx/xxx/xxx/xx/xxxxxxx" -prune -o \
              -path "$(HOME_PATH)/xxx/xxx/xxx/xxx/xxx" -prune -o \
              -path "$(HOME_PATH)/xxx/xxx/xxx/xxx/xxx" -prune -o \
			  -path "$(HOME_PATH)/TLClass" -prune -o
rwildcard := $(shell find $(HOME_PATH) $(ignore_dir) -name "*.*")

TLCLASS_FILES := $(shell find $(HOME_PATH)/TLClass -name "*.*")
TLCLASS_ALL_FILES := $(foreach src_path,$(TLCLASS_FILES), ./$(src_path)) 
TLCLASS_CPP := $(filter $(MY_FILES_SUFFIX_CPP),$(TLCLASS_ALL_FILES))

# 获取相应的源文件
MY_ALL_FILES := $(foreach src_path,$(rwildcard), ./$(src_path)) 
SOURCE_CPP  := $(filter $(MY_FILES_SUFFIX_CPP),$(MY_ALL_FILES))
SOURCE_C    := $(filter $(MY_FILES_SUFFIX_C),$(MY_ALL_FILES))
SOURCE_CXX  := $(filter $(MY_FILES_SUFFIX_CXX),$(MY_ALL_FILES))


%.o : %.c
	$(CC) $(CPPFLAGS) $(CPPFLAGS_INC) $(CPPFLAGS_HIDDEN) -c -o $@ $^ 

%.o : %.cxx
	$(CC) $(CPPFLAGS) $(CPPFLAGS_INC) $(CPPFLAGS_HIDDEN) -c -o $@ $^ 

%.o : %.cpp
	$(CC) $(CPPFLAGS) $(CPPFLAGS_INC) $(CPPFLAGS_HIDDEN) -c -o $@ $^ 

#源码目录下
OBJS_CPP = $(SOURCE_CPP:%.cpp=./%.o)
OBJS_C   = $(SOURCE_C:%.c=./%.o)
OBJS_CXX = $(SOURCE_CXX:%.cxx=./%.o)
OBJS = $(OBJS_CPP) $(OBJS_C) $(OBJS_CXX)

export CPPFALGS_GXIAPI_PATH=$(CPPFLAGS)
export CC_GXIAPI_PATH=$(CC)
export AR_GXIAPI_PATH=$(AR)

.PHONY : all cxx objs clean rebuild

all : $(DESTINATION)

objs : $(OBJS)

clean :
	@$(RM) $(OBJS)
	@$(RM) xxx.a
	@$(RM) $(DESTINATION)
	@$(RM) $(HOME_PATH)/xxx/xxx/xxx.a
	@cd $(HOME_PATH)/xxx/xxx/ && make clean

rebuild: clean all 

$(DESTINATION) : $(OBJS) $(TLCLASS_CPP)
	cd $(HOME_PATH)/xxx/makefile/ && make -j8
	$(CC) $(CPPFLAGS) -o $@ $(OBJS) $(addprefix -l,$(LIBS)) -L$(HOME_PATH)/xxx/makefile/ -ltlclass
	$(AR) -r xxx.a $(OBJS) 
	@echo "**********************************"
	@echo "Generate xxx.so success!"
	@echo "**********************************"

个人疑惑

%.o : %.c 这个是用来干嘛的呢?

你可能感兴趣的:(Linux,基础,linux)