GCC makefile依赖关系生成路径 [英] GCC makefile dependency generation path

查看:139
本文介绍了GCC makefile依赖关系生成路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC中的 -MM 标志为对象生成makefile依赖关系。 makefile简单的看起来像这样:

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

来源位于文件夹 src 。但是, autodep 文件将包含没有相对路径的对象目标:

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



我应该如何将它们变成这样:

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



我试着用 -MT flag,但它似乎完全放弃了对象目标。

解决方案

-MT 设置整个目标名称。如果你想为每个源指定一个不同的目标,你需要为每个源指定一个不同的 -MT 参数,这意味着编译器和foreach循环的多重调用:


$ b $

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

或者,您可以使用sed按摩输出



<$ $($)$($)$($)$($)$($)$($)$($)$(CC)来源)| sed's | ^ | src / |'> autodep

更容易的是将每个源文件的依赖关系放入它自己的.d文件中,并使用 -MMD 标志在您编译源文件时生成:

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

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


I use the -MM flag in GCC to generate makefile dependencies for objects. 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. However, the autodep file will contain the object targets without their relative path:

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 sets the entire target name. 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:

$(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

$(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:

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

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

这篇关于GCC makefile依赖关系生成路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆