In a makefile, how to get the relative path from one absolute path to another?

前端 未结 7 2104
别那么骄傲
别那么骄傲 2021-02-19 20:57

An example to illustrate my question:

Top level makefile

rootdir = $(realpath .)
export includedir = $(rootdir)/include
default:
    @$(MAKE) --directory         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 21:21

    Didier's answer is the best one, but the following might give you some ideas:

    includedir=/a/b/c/d
    currentdir=/a/b/e/f/g
    up=; while ! expr $includedir : $currentdir >/dev/null; do up=../$up; currentdir=`dirname $currentdir`; done; relative=$up`expr $includedir : $currentdir'/*\(.*\)'`
    echo "up=$up  currentdir=$currentdir, relative=$relative"
    

    Sorted!

    (no-one said it had to be pretty...)

提交回复
热议问题