博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在linux中运行中定义lib的路径在makefile里面
阅读量:4167 次
发布时间:2019-05-26

本文共 5220 字,大约阅读时间需要 17 分钟。

场景:比如执行程序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

 

转载地址:http://tuexi.baihongyu.com/

你可能感兴趣的文章
A Game of Thrones(97)
查看>>
A Game of Thrones(98)
查看>>
2018.3.20
查看>>
2018.3.21
查看>>
2018.3.22
查看>>
2018.3.23
查看>>
A Game of Thrones(102)
查看>>
2018.4.29
查看>>
2018.4.30
查看>>
2018.4.31
查看>>
2018.4.32
查看>>
2018.4.33
查看>>
《python基础教程》答案(第一章)
查看>>
2018.4.34
查看>>
2018.4.35
查看>>
2018.4.36
查看>>
我为什么要写博客
查看>>
如何导入pycharm无法导入的包
查看>>
2018.4.37
查看>>
2018.4.38
查看>>