【问题标题】:Gnu Make's recursiveness and friendsGnu Make的递归和朋友
【发布时间】:2013-07-20 01:06:46
【问题描述】:

也许我做错了什么,但是当 makefile 看起来像这样时,make 拒绝检查依赖项的时间戳。

# This makefile won't update the objects if you modify the .cpp files
# and it will only create them if they do not exist.
CC=g++
FL=-g
OBJECTD=../obj
SOURCED=../src

# Get all .cpp files in ../src
SOURCES=$(wildcard *.cpp)

# Convert .cpp to .o then add ../obj in front
OBJECTS=$(addprefix $(OBJECTD)/,$(patsubst %.cpp,%.o,$(SOURCES)))

# You sir must execute yourself whether you like it or not
.PHONY:all

all:$(OBJECTS)
    @echo : Leaving `src`


# This wont work as expected ...
# @echo : Compiling $(notdir $(patsubst %.o,%.cpp,$@))
# echoes Compiling logger.cpp
# But $(notdir $(patsubst %.o,%.cpp,$@)) will not be parsed
# correctly
$(OBJECTS):$(notdir $(patsubst %.o,%.cpp,$@))
    @echo : Compiling $(notdir $(patsubst %.o,%.cpp,$@))
    @$(CC) $(FL) -c $(notdir $(patsubst %.o,%.cpp,$@)) -o $@

我相信它不会接受$(notdir $(patsubst %.o,%.cpp,$@))

即使文档声明

在具有多个目标的模式规则中(请参阅模式规则简介),'$@' 是导致规则的目标的名称 要运行的配方。


分别对每个文件进行编译,效果很好。
例如:

../obj/logger.o:logger.cpp
    @echo : Compiling $<
    @$(CC) $(FL) -c $< -o $@


+------------+
: 额外信息:
+------------+


我想要实现的是从当前目录读取源代码,检查它们是否已更改,然后继续编译它们,同时将对象放在 ../obj 而不是当前目录。
这是位于 ../src 中的makefile,在父目录中还有一个用于链接。


我想要处理的结构的图形表示

 +--- Parent Directory ------+
 |        |        |         |
 |        |        |         |
bin      obj      inc       src

【问题讨论】:

    标签: c++ recursion makefile gnu


    【解决方案1】:

    你的规则:

    $(OBJECTS):$(notdir $(patsubst %.o,%.cpp,$@))
        ...
    

    不起作用,因为$@ 在先决条件列表中没有值。来自the manual

    [自动变量]不能在一个先决条件列表中直接访问 规则。

    试试this

    $(OBJECTS): $(OBJECTD)/%.o : %.cpp
        @echo : Compiling $<
        @$(CC) $(FL) -c $< -o $@
    

    【讨论】:

    • @TheOtherGuy:这里有三个新想法:1)$@ 在先决条件列表中不起作用,2)静态模式规则,3)$&lt; 表示第一个先决条件。哪一个给你带来了麻烦?
    • 我得到了 $
    • 点击链接(“this”)了解静态模式规则。
    • 阅读:gnu.org/software/make/manual/html_node/…,非常感谢:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多