在makefile中,如何获取从一个绝对路径到另一个绝对路径的相对路径? [英] In a makefile, how to get the relative path from one absolute path to another?

查看:455
本文介绍了在makefile中,如何获取从一个绝对路径到另一个绝对路径的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个例子来说明我的问题:

An example to illustrate my question:

顶级makefile

Top level makefile

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

src/libfoo的Makefile

Makefile for src/libfoo

currentdir = $(realpath .)
includedir = $(function or magic to make a relative path
               from $(currentdir) to $(includedir),
               which in this example would be ../../../include)

另一个例子:

current dir = /home/username/projects/app/trunk/src/libs/libfoo/ 
destination = /home/username/projects/app/build/libfoo/ 
relative    = ../../../../build/libfoo

在尝试尽可能便携的同时如何做到这一点?

How can this be done, while trying to be as portable as possible?

推荐答案

做您想做的事情看起来并不容易.可能会在makefile中使用很多组合的$(if,但不便于移植(仅适用于gmake),而且麻烦.

Doing what you want does not look easy. It may be possible using a lot of combined $(if in the makefile but not portable (gmake only) and cumbersome.

恕我直言,您正在尝试解决自己创建的问题.为什么不从顶级Makefile发送正确的includedir值作为相对路径?可以很容易地完成以下操作:

IMHO, you are trying to solve a problem that you create yourself. Why don't you send the correct value of includedir as a relative path from the Top-level Makefile? It can be done very easily as follows:

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

然后,您可以在子makefile中使用$(includedir).它已经被定义为相对的.

Then you can use $(includedir) in the sub-makefiles. It is already defined as relative.

这篇关于在makefile中,如何获取从一个绝对路径到另一个绝对路径的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆