c++ - Gnu Make 的递归性和 friend

标签 c++ recursion makefile gnu

也许我做错了什么,但是当 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,$@))

即使文档声明

In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ‘$@’ is the name of whichever target caused the rule's recipe to be run.


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

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


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


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


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

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

最佳答案

您的规则:

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

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

[Automatic variables] cannot be accessed directly within the prerequisite list of a rule.

尝试 this :

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

关于c++ - Gnu Make 的递归性和 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757632/

相关文章:

c++ - 没有模板参数?

java - 递归爬楼梯算法

linux - make : Command not found, 但命令存在?

php - 递归函数在 codeIgniter 中不起作用

twitter-bootstrap - 如何构建 Twitter Bootstrap

c++ - 为 iOS 5.0 编译/链接 FreeImage

c++ - 使用 boost::mpl 的类型列表的排列

c++ - 为什么析构函数默认不是虚拟的[C++]

c++ - 空字符串和 '\0' 字符有什么区别? (从指针和数组的角度来看)

javascript - 递归 promise