8

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/ \
Drise
  • 4,310
  • 5
  • 41
  • 66
  • Weird behavior as random bits of code that I haven't modified in ages suddenly throwing errors, or even library code throwing errors internal to itself... – Drise May 17 '12 at 14:34
  • It sounds like you are missing some dependencies on the headers. – juanchopanza May 17 '12 at 14:40
  • A note to consider: My make file is automatically generated by QMake, based on my Qt .pro file. Modifying my Makefile is somewhat not an option, because when the .pro file changes, the Makefile is recreated. – Drise May 17 '12 at 15:16

5 Answers5

5

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
leemes
  • 44,967
  • 21
  • 135
  • 183
  • That's the answer! The Makefile generated from a QMake file automatically updates itself when "make" is run and a new header file has been added to the QMake file. However, if the new header file is not included in the .cpp files already at that point, no dependency update is computed, and qmake needs to be rerun manually afterwards. – DCTLib Mar 27 '14 at 14:58
3

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

nhed
  • 5,774
  • 3
  • 30
  • 44
  • 1
    I believe you had the best answer, because it did lead me to read more about QMake's generation system, and how to set dependencies within Qt .pro files. – Drise May 17 '12 at 15:43
2

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.

Dave S
  • 20,507
  • 3
  • 48
  • 68
  • havent used it in years, `makedepend` can seriously slow down your compilation ... so I'd say stay away when possible – nhed May 17 '12 at 15:02
1

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.

pippin1289
  • 4,861
  • 2
  • 22
  • 37
  • An additional note, is if you set your dependencies correctly in your make system, you don't always need to make clean every time, rather only when you change what is included within a .h file. – pippin1289 May 17 '12 at 14:41
  • I do most of my work in .cpp files. However, If I need to add a new function to a class, or add a new private variable, I must change that in the header. – Drise May 17 '12 at 14:41
  • Of course, so then because of dependencies anything which includes this will be recompiled, so reducing the dependencies between files can help. Such as being careful of including in .h files unnecessarily cause that can create a dependency in other files. I usually try to only include in .cpp when I can. – pippin1289 May 17 '12 at 14:43
  • This is also true for my program. I only `#include` when I need to. – Drise May 17 '12 at 14:45
1

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

Ulterior
  • 2,786
  • 3
  • 30
  • 58