简体   繁体   English

GCC makefile依赖项生成路径

[英]GCC makefile dependency generation path

I use the -MM flag in GCC to generate makefile dependencies for objects. 我在GCC中使用-MM标志来生成对象的makefile依赖性。 The makefile briefly looks like this: 生成文件简要如下所示:

-include autodep
...
$(TARGET): build $(OBJECTS)
    $(CC) -shared -o $@ $(OBJECTS)
    $(CC) -MM $(SOURCES) > autodep

The sources are located the in folder src . 源位于src文件夹中。 However, the autodep file will contain the object targets without their relative path: 但是, autodep文件将包含对象目标,而没有它们的相对路径:

foo.o: src/foo.c src/foo.h
bar.o: src/bar.c src/bar.h src/baz.h

How should I turn them into this: 我应该如何将它们变成这样:

src/foo.o: src/foo.c src/foo.h
src/bar.o: src/bar.c src/bar.h src/baz.h

?

I tried using the -MT flag, but it seems to discard object targets altogether. 我尝试使用-MT标志,但似乎完全放弃了对象目标。

-MT sets the entire target name. -MT设置整个目标名称。 If you want a different target for each source, you need a different -MT argument for each source, which means multiple invocations of the compiler and a foreach loop: 如果每个源都想要一个不同的目标,则每个源都需要一个不同的-MT参数,这意味着需要多次调用编译器和一个foreach循环:

$(TARGET): build $(OBJECTS)
    $(CC) -shared -o $@ $(OBJECTS)
    rm autodep
    $(foreach SRC,$(SOURCES),$(CC) -MM -MT $(SRC:.c=.o) $(SRC) >> autodep;)

Alternately, you can use sed to massage the output 或者,您可以使用sed按摩输出

$(TARGET): build $(OBJECTS)
    $(CC) -shared -o $@ $(OBJECTS)
    $(CC) -MM $(SOURCES) | sed 's|^|src/|' > autodep

Easier still is to put the dependencies for each source file into it own .d file and use the -MMD flag to generate that when you compile the source file: 更简单的方法是将每个源文件的依赖项放入其自己的.d文件中,并在编译源文件时使用-MMD标志生成该-MMD

-include $(SOURCES:.c=.d)
CFLAGS += -MMD

$(TARGET): build $(OBJECTS)
    $(CC) -shared -o $@ $(OBJECTS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM