通用GNU Makefile目录路径 [英] Common GNU makefile directory path

查看:78
本文介绍了通用GNU Makefile目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用通用的makefile来合并一些构建信息.我的问题是我想使用来自不同子目录级别的该makefile,这使得工作目录值(pwd)不可预测.例如:

I'm trying to consolidate some build information by using a common makefile. My problem is that I want to use that makefile from different subdirectory levels, which makes the working directory value (pwd) unpredictable. For example:

# Makefile.common
TOP := $(shell pwd)
COMPONENT_DIR := $(TOP)/component
COMPONENT_INC := $(COMPONENT_DIR)/include
COMPONENT_LIB := $(COMPONENT_DIR)/libcomponent.a

如果像这样从子目录中包含Makefile.common,则$(TOP)目录不正确,其他所有内容也将适用:

If I include Makefile.common from a subdirectory, like so, the $(TOP) directory is incorrect and everything else follows suit:

# other_component/Makefile
include ../Makefile.common
# $(COMPONENT_LIB) is incorrectly other_component/component

Makefile.common使用自己的 目录路径而不是更易变的pwd的最佳方法是什么?

What's the best way to get Makefile.common to use its own directory path instead of the more fickle pwd?

推荐答案

您应该可以使用

You should be able to use the MAKEFILE_LIST variable, like this:

# This must be the first line in Makefile.common
TOP := $(dir $(firstword $(MAKEFILE_LIST)))

从文档中:

当make读取各种makefile时,包括从MAKEFILES变量,命令行,默认文件或include指令获取的任何makefile,它们的名称都会自动附加到MAKEFILE_LIST变量中.它们是在make开始解析它们之前添加的.这意味着,如果makefile要做的第一件事是检查此变量中的最后一个单词,它将是当前makefile的名称.但是,一旦当前的makefile使用了include,最后一个字就是刚刚包含的makefile.

As make reads various makefiles, including any obtained from the MAKEFILES variable, the command line, the default files, or from include directives, their names will be automatically appended to the MAKEFILE_LIST variable. They are added right before make begins to parse them. This means that if the first thing a makefile does is examine the last word in this variable, it will be the name of the current makefile. Once the current makefile has used include, however, the last word will be the just-included makefile.

这篇关于通用GNU Makefile目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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