在makefile中,如何获取从一个绝对路径到另一个绝对路径的相对路径?

在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.