Make doesn't rebuild headers when changed

跟風遠走 提交于 2019-12-06 23:41:44

问题


I have a project for which I regularly modify headers and when I do so, and forget to make clean then make, I get all sorts of weird behavior. I'm currently using Qt Creator as my IDE, but I've seen this happen on a Qt-independent project. My project is getting fairly large, and having to rebuild every time I make a header change is becoming unproductive. Any thoughts?

For future reference:

If using the QMake system:

DEPENDPATH += . \
    HeaderLocation1/ \
    HeaderLocation2/ \
    HeaderLocation2/HeaderSubLocation1/ \
    HeaderLocation2/HeaderSubLocation2/ \
    HeaderLocation2/HeaderSubLocation3/ \
    HeaderLocation2/HeaderSubLocation4/ \
    HeaderLocation2/HeaderSubLocation5/ \
    HeaderLocation3/ \
    HeaderLocation3/HeaderSubLocation1/ \
    HeaderLocation3/HeaderSubLocation2/ \

回答1:


Dont know anything about your IDE, and not sure if its relevant, and since you are not including your makefile - I'll just state the obvious - do you use any auto-generated dependencies?

For g++ I use the -MD flag, then in the makefile include $(wildcard bin/*.d) (depending on where your object file is created, mine are created in 'bin')

Also make sure to delete the dep file on a clean build




回答2:


Re-run qmake. This will generate a new Makefile which will have proper dependencies.

Example:

A file file.h looking like the following:

#include "some.h"
#include "header.h"
#include "files.h"
...

and file.cpp looking like the following:

#include "file.h"
...

and having in your .pro:

HEADERS += file.h some.h header.h files.h
SOURCES += file.cpp

will produce the following in the resulting Makefile:

file.o: ../src/file.cpp ../src/file.h \
        ../src/some.h \
        ../src/header.h \
        ../src/files.h
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o file.o ../src/file.cpp



回答3:


The solution is to have proper header dependency's in your makefile.

1) Use makedepend to generate the dependency files. You would add a target to your makefile which regenerates the dependency file, and you'd want to invoke that before actually doing your compilation. 2) GCC only: Use '-MMD' and '-MP' options on your compile line for .c/.cpp files. This causes GCC to generate a dependency file for input file. Then, you can include these in your makefile. The advantage here is that with those two options, as you add and remove headers, it should behave as you expect.




回答4:


This is mainly caused by dependencies between files. So if you alter a .h file which is included in another, that file will also need to be recompiled. So either you need to reduce your includes, or do your coding in .h/.cpp form when possible to make changes in .cpp more often than .h.




回答5:


Here is an excerpt from my own makefile generating and using dependency files automatically created during compilation. You would have to make an additional entry for cpp files

%.o : %.c
    $(CC) -M $(CFLAGS) -o $*.P $<
    @cp $*.P $*.d; \
            sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
                -e '/^$$/ d' -e 's/$$/ :/' < $*.P >> $*.d; \
            rm -f $*.P
    $(CC) $(CFLAGS) -c $< -o $@

-include *.d

Dont forget to add delete *.d files on clean build



来源:https://stackoverflow.com/questions/10637762/make-doesnt-rebuild-headers-when-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!