簡體   English   中英

包含另一個Makefile會導致構建失敗,為什么?

[英]Including another Makefile results in a failed build, why?

問題

包含外部Makefile會使我以前穩定的構建失敗,並出現以下錯誤:

make:***沒有規則將目標“ build / main.o”設置為“ all”所需。 停止。

語境

上下文是,我目前管理每個項目的構建,每個項目有一個Makefile。 由於項目的Makefile具有很多冗余,因此我想將常見的東西放在一個外部Makefile中,該文件將包含在每個項目的Makefile中。

重現問題的最小示例

在這個簡單而簡約的示例中,我嘗試將srcs / main.c構建到build / main.o中,並且還嘗試顯示變量FOO里面的內容,其中是tools.mk

資料夾結構:

|   Makefile
|
+---build
|       (main.o)
+---mkf
|       tools.mk
|
\---srcs
        main.c

Makefile的內容:

include ./mkf/tools.mk

mkfile_path :=$(realpath $(lastword $(MAKEFILE_LIST)))
current_dir :=$(dir $(mkfile_path))

VPATH = $(current_dir)/srcs
./build/%.o: %.c
    @echo $< $@
    gcc $< -o $@

all: ./build/main.o test
    @echo Done ! 

test:
    @echo Testing include : $(FOO)  

.PHONY: all test

tools.mk的內容:

FOO = 42

main.c的內容(基本的hello world):

# include <stdio.h>
# include <stdlib.h>

int main(void)
{   
    printf("Hello, world!");
    return EXIT_SUCCESS;
} /*main*/

現在基本上我的問題是,如果我將自己放在根文件夾中並鍵入make all ,則構建將因上述錯誤而失敗。 但是,如果我注釋該行include ./mkf/tools.mk則構建成功。 因此,我猜它由於包含行而失敗,但是我無法弄清原因。

有人可以啟發我嗎?

Windows 7 64-bits上使用GNU Make 4.2進行構建。

mkfile_path :=$(realpath $(lastword $(MAKEFILE_LIST)))

這樣就產生了最后一個包含的makefile的路徑,該文件是./mkf/tools.mk 有關詳細信息,請參見其他特殊變量

解決方法:

mkfile_path :=$(realpath $(lastword $(MAKEFILE_LIST)))
current_dir :=$(dir $(mkfile_path))
include ./mkf/tools.mk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM