Make doesn't rebuild headers when changed

2019-04-23 23:32发布

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/ \

5条回答
Ridiculous、
2楼-- · 2019-04-24 00:07

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.

查看更多
乱世女痞
3楼-- · 2019-04-24 00:22

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.

查看更多
Deceive 欺骗
4楼-- · 2019-04-24 00:25

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
查看更多
等我变得足够好
5楼-- · 2019-04-24 00:31

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

查看更多
Luminary・发光体
6楼-- · 2019-04-24 00:34

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

查看更多
登录 后发表回答