可以在替换宏中使用makefile内部宏

时间:2018-09-26 05:42:50

标签: makefile

我一直在学习如何创建make文件并了解基本语法。我使用了几本在线指南和手册,将我最能理解的工作文件拼凑在一起,但是一个目标行的含义难以理解,因此找不到与该行相似的指南。令我困惑的是,我无法确定用于替换宏的内部宏是否代表当前目标$ @?有可能吗?

以下一行令我感到困惑:

$(OBJ): $(@:=.o)

要添加适当的上下文,我包括我的makefile。

# Include varialbes for make file acts like similar to the cpp directive
# (non standard to SUS)
include ../build/config.mk

# Define known suffixes
.SUFFIXES: .o .c

# Define include directory macro for header files
INCLUDE = -I../include

# Define source file macro list
SRC =\
    base32.c\
    base64.c\
    change_file_own.c\
    colorize-ls.c\
    comma_list.c\
    datetime.c\
    defs.c\
    error.c\
    escape.c\
    filebuffer.c\
    fileinfo.c\
    filestat.c\
    format.c\
    halt_getopt.c\
    head_tail.c\
    io.c\
    line.c\
    mv-rm.c\
    numbers.c\
    path.c\
    perm.c\
    printf_format.c\ 
    progname.c\ 
    signalinfo.c\
    stack.c\
    uuencode.c\ 
    wrappers.c\ 
#       move.c\

# Define object list to be everything in SRC macro with any instance
# of .c replaced with .o
OBJ = $(SRC:.c=.o)

# Define final product name
LIB = libutil.a

# Default target including $(LIB) (libutil.a) as a required component
all: $(LIB)

# ???? 
$(OBJ): $(@:=.o)

# SUFFIX rule to transform .c files to .o files
# Compile each precursor c file ($<) and output target object files ($@)
.c.o:
        $(CC) $(CFLAGS) $(INCLUDES) -o $@ -c $<

# Target libutil with component object files
# Run archiver inserting newer than target component object files ($?)
# into and or creating the archive ($@)
$(LIB): $(OBJ)
        $(AR) rcs $@ $?

# Clean up the object files
clean:
        rm -f $(OBJ) $(LIB)
# define fake (non file) target names
# (also might be non-standard to SUS)
.PHONY:
        all clean

config.mk文件包括CFLAGS以及到存档器和编译器的正确路径。

1 个答案:

答案 0 :(得分:0)

在您的$(OBJ): $(@:=.o)中,替换项$(@:=.o)将扩展为空。

请参见手册10.5.3 Automatic Variables

  

认识到自动变量值可用的有限范围非常重要:   他们只有配方中的值。特别是,您不能在规则的目标列表中的任何位置使用它们。   它们在那里没有值,并将扩展为空字符串。同样,不能在规则的先决条件列表中直接访问它们。   一个常见的错误是尝试在先决条件列表中使用$ @。这是行不通的。   但是,GNU make有一个特殊功能,即辅助扩展(请参阅“辅助扩展”),它将允许自动变量值在先决条件列表中使用。