makefile自動依賴

來源:互聯網
上載者:User

makefile檔案(1)修改define COMPILE_C下面的
$(CC) $(INCDIRS) $(CFLAGS) $(RMCFLAGS) -c $<
為
$(CC) $(INCDIRS) $(CFLAGS) $(RMCFLAGS) -MD -c $<

修改define COMPILE_CPP下面的
$(CPP) $(INCDIRS) $(CPPFLAGS) $(RMCFLAGS) -c $<
為
$(CPP) $(INCDIRS) $(CPPFLAGS) $(RMCFLAGS) -MD -c $< 說明:用於產生依賴檔案*.d

(2)增加DEPENDFILES = $(patsubst %.cpp,%.d,$(SOURCEFILES))說明:將.cpp的檔案替換為.d(3)在檔案末尾增加-include $(DEPENDFILES)說明:makefile檔案中包含這些.d檔案,從而實現自動依賴。前面“-”必不可少,因為第一次編譯時間沒有.d檔案
(4)修改clean命令為clean:
     rm -f $(TARGET) $(OBJECTFILES) $(DEPENDFILES)

附錄:
-MF FILE
    When used with -M or -MM, specifies a file to write the dependencies to.  If no -MF switch is given the preprocessor sends
the rules to the same place it would have sent preprocessed output.

    When used with the driver options -MD or -MMD, `-MF overrides the default dependency output file.

-MD
    -MD is equivalent to -M -MF FILE, except that -E is not implied.  The driver determines FILE based on whether an `-o option
is given.  If it is, the driver uses its argument but with a suffix of .d, otherwise it take the basename of the input file and applies a .d suffix.

    If -MD is used in conjunction with -E, any -o switch is understood to specify the dependency output file (but *note -MF:
    dashMF.), but if used without -E, each -o is understood to specify a target object file.

    Since -E is not implied, -MD can be used to generate a dependency output file as a side-effect of the compilation process.

-MMD
    Like -MD except mention only user header files, not system header files.


-MP
    This option instructs CPP to add a phony target for each dependency other than the main file, causing each to depend on
nothing.  These dummy rules work around errors make gives if you remove header files without updating the Makefile to match.

    This is typical output:

         test.o: test.c test.h

         test.h:

-MT TARGET
    Change the target of the rule emitted by dependency generation.  By
    default CPP takes the name of the main input file, including any
    path, deletes any file suffix such as .c, and appends the
    platforms usual object suffix.  The result is the target.

    An -MT option will set the target to be exactly the string you
    specify.  If you want multiple targets, you can specify them as a
    single argument to -MT, or use multiple -MT options.

    For example, -MT $(objpfx)foo.o might give

         $(objpfx)foo.o: foo.c 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.