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

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

An example to illustrate my question:

Top level makefile

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


        
7条回答
  •  猫巷女王i
    2021-02-19 21:20

    Perl is portable! Here's a solution using the abs2rel function from the core Perl module File::Spec (resp. File::Spec::Functions):

    current_dir= /home/username/projects/app/trunk/src/libs/libfoo/ 
    destination= /home/username/projects/app/build/libfoo/ 
    relative=    $(shell perl -MFile::Spec::Functions=abs2rel -E 'say abs2rel(shift, shift)' $(destination) $(current_dir))
    
    makefile_target:
        @echo "Use now $(relative)"
    

提交回复
热议问题