場景:比如執行程序testlog需要用到liblog.so,但是testlog在運行當中提示找不到所需要的動態庫文件liblog.so(放在了/media/dc/mt_tmnl698/lib_zt目錄)
此時需要在編譯testlog時的makefile需要改進如下:紅色字體 定義運行時的路徑
TARGET_LIB_DIR =/media/dc/mt_tmnl698/lib_zt
TARGET_SHARED_LIBS := -llog
TARGET_SHARED_LIBS += -Wl,-rpath=$(strip $(TARGET_LIB_DIR))
HOST_NAME := win32
#ifndef MakeTool
#ifeq ($(target),ubuntu)
#include ../Makefile.tool.ubuntu
#else
#ifeq ($(target),9g25)
#include ../Makefile.tool.9g25
#else
#ifeq ($(host),ubuntu)
#include ../Makefile.tool.ttu.ubuntu
#else
#include ../Makefile.tool.ttu.win32
#endif
#endif
#endif
#endif
#[Makefile.tool.ttu.win32,Makefile.tool.9g25,Makefile.tool.ubuntu]
include ../../../Makefile.tool
#include ../Makefile.tool.ubuntu
#include ../Makefile.tool.9g25
# current directory
CURDIR := $(shell pwd)
# build directory
ifndef BLDDIR
BLDDIR := $(CURDIR)/build
endif
TARGET_OUT_DIR := $(MT_RELEASE_BASE)/app/dc
TARGET_LIB_DIR =/media/dc/mt_tmnl698/lib_zt
ifeq ($(findstring win32,$(HOST_NAME)),win32)
BLDDIR := $(subst /,\,$(BLDDIR))
TARGET_OUT_DIR := $(subst /,\,$(TARGET_OUT_DIR))
endif
# source directory--------------------------------------------[need modify]
SRCDIRS := ./src
# define souce file search directory
VPATH = $(SRCDIRS)
# define make output file directory
OUT_DIR:=$(BLDDIR)
#define .o file directory
OBJ_DIR:=$(BLDDIR)/$(shell basename $(CURDIR))_objs
OBJDIR := $(OBJ_DIR)
ifeq ($(findstring win32,$(HOST_NAME)),win32)
OBJDIR := $(subst /,\,$(OBJ_DIR))
endif
#define module name-------------------------------------------[need modify]
TARGET_NAME := test_log$(TARGET_SUFFIX)
#define make target type:[program,arch,so]--------------------[need modify]
TARGET_FILES := program
PROGRAM := $(OUT_DIR)/$(TARGET_NAME)
ARCH := $(OUT_DIR)/lib$(TARGET_NAME).a
DYN_LIB := $(OUT_DIR)/lib$(TARGET_NAME).so
# define compile option----------------------------------------[need modify]
#CFLAGS := $(C_FLAGS)
ifeq ($(findstring so,$(TARGET_FILES)),so)
CFLAGS += -fpic
endif # endif{ifeq($findstring($(TARGET_TYPE),so),so)}
# define compile option generate .d file
DEPENDFLAG := -MM
# define make arch files option
ARFLAGS := $(AR_FLAGS)
# define system shared lib directory
SYS_SO_DIR := $(foreach dir,$(LINUX_LIB_DIR),-L$(dir))
# target shared lib directory-------------------------------------[need modify]
TARGET_SHARED_DIR := -L$(MT_LIB_RELEASE)/lib -L$(TARGET_OUT_DIR)
# link user shared lib------------------------------------------[need modify]
TARGET_SHARED_LIBS := -llog
TARGET_SHARED_LIBS += -Wl,-rpath=$(strip $(TARGET_LIB_DIR))
# user include directory----------------------------------------[need modify]
TARGET_INC_DIR := -I./src -I./inc -I../include/inc_plat -I../include
# link user static lib------------------------------------------[need modify]
TARGET_STATIC_LIBS :=
CFLAGS += $(TARGET_INC_DIR)
TARGET_LD_FLAGS := $(TARGET_STATIC_LIBS) $(TARGET_SHARED_DIR) $(TARGET_SHARED_LIBS)
TARGET_LD_FLAGS += $(LDLIBS)
#source files
SRCS=$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c))
#obj files
OBJS=$(addprefix $(OBJ_DIR)/,$(notdir $(patsubst %.c,%.o,$(SRCS))))
#depend files=.d files
DEPENDS=$(addprefix $(OBJ_DIR)/,$(notdir $(patsubst %.c,%.d,$(SRCS))))
ifneq ($(findstring program,$(TARGET_FILES)),program)
PROGRAM :=
endif
ifneq ($(findstring so,$(TARGET_FILES)),so)
DYN_LIB :=
endif
ifneq ($(findstring arch,$(TARGET_FILES)),arch)
ARCH :=
endif
#target all
.PHONY:all
all:$(PROGRAM) $(ARCH) $(DYN_LIB)
#generate .d files
#$(DEPENDS):$(OBJ_DIR)/%.d:%.c
# test -d $(BLDDIR) || mkdir $(BLDDIR)
# test -d $(OBJDIR) || mkdir $(OBJDIR)
# $(CC) $(DEPENDFLAG) $(CFLAGS) $< | \
# sed "s?\\(.*\\):?$(addprefix $(OBJ_DIR)/,$(notdir $(basename $<))).o \
# $(addprefix $(OBJ_DIR)/,$(notdir $(basename $<))).d :?g" \
# > $@ || $(RM) $@
#compile source files
$(OBJS):$(OBJ_DIR)/%.o:%.c
test -d $(BLDDIR) || mkdir $(BLDDIR)
test -d $(OBJDIR) || mkdir $(OBJDIR)
$(CC) -c $< -o $@ $(CFLAGS)
# make program
ifeq ($(findstring program,$(TARGET_FILES)),program)
$(PROGRAM): $(OBJS)
$(CC) -o $@ $(filter %.o ,$+) $(TARGET_LD_FLAGS)
ifeq ($(findstring win32,$(HOST_NAME)),win32)
$(COPY) $(subst /,\,$@) $(TARGET_OUT_DIR)
else
$(COPY) $@ $(TARGET_OUT_DIR)
endif
endif
# make sharelib .so
ifeq ($(findstring so,$(TARGET_FILES)),so)
$(DYN_LIB):$(OBJS)
$(LD) -shared -o $@ $(filter %.o ,$+) -L$(LINUX_LIB_DIR)/../lib $(TARGET_LD_FLAGS)
ifeq ($(findstring win32,$(HOST_NAME)),win32)
$(COPY) $(subst /,\,$@) $(TARGET_OUT_DIR)
$(COPY) $(subst /,\,$@) ..\lib
$(COPY) .\inc\*.h ..\include
else
$(COPY) $@ .$(TARGET_OUT_DIR)
$(COPY) $@ ../lib/
$(COPY) ./inc/*.h ../include
endif
endif
# make arch .a
ifeq ($(findstring arch,$(TARGET_FILES)),arch)
$(ARCH):$(OBJS)
test -d $(BLDDIR) || mkdir $(BLDDIR)
test -d $(OUT_DIR) || mkdir $(OUT_DIR)
$(AR) $(ARFLAGS) $(ARCH) $(OBJS)
$(RANLIB) $@
ifeq ($(findstring win32,$(HOST_NAME)),win32)
$(COPY) $(subst /,\,$@) ..\lib
$(COPY) .\inc\*.h ..\include
else
$(COPY) $@ ../lib/
$(COPY) ./inc/*.h ../include
endif
endif
# include .d files
#include $(DEPENDS)
# make clean
.PHONY:clean
clean:
$(RM) $(OBJS) $(DEPENDS)
ifeq ($(findstring program,$(TARGET_FILES)),program)
$(RM) $(PROGRAM)
endif
ifeq ($(findstring so,$(TARGET_FILES)),so)
$(RM) $(DYN_LIB)
endif
ifeq ($(findstring arch,$(TARGET_FILES)),arch)
$(RM) $(ARCH)
endif