且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GCC makefile依赖关系生成路径

更新时间:2023-02-07 13:05:45

-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)

相关阅读

技术问答最新文章