0

THE FILES

foo.hpp

#include <iostream>
class foo
{
public:
    foo(int = 1);
    int get_x() const { return x; }
private:
    int x;
};

foo.cpp

#include "foo.hpp"
foo::foo(int xp)
{
    x = xp;
}

main.cpp

#include "foo.hpp"
int main()
{
    foo bar;
    std::cout << bar.get_x() << std::endl;
}

Makefile (this is overkill for such a simple example, but I want to use something like this)

SHELL = /bin/bash
CC = g++
CPPFLAGS = -MMD -MP
SRC = $(wildcard *.cpp)
OBJ = $(SRC:.cpp=.o)
EXECUTABLE = main

all: $(SRC) $(EXECUTABLE)

$(EXECUTABLE): $(OBJ)
    $(CC) $(OBJ) -o $@

.cpp.o:
    $(CC) $(CPPFLAGS) -c $^

-include $(SRC:.cpp=.d)

OBSERVED BEHAVIOR
The files are 20K. Run make, output is 1 and files are 48K. Now change default argument int = 1 in header file to int = 2.
make: output is 2 and files are 11M. Nearly all of that is in foo.hpp.gch. Change int = 2 to int = 3.
make: output is still 2, foo.hpp.gch did not update.

Now if I move #include <iostream> out of foo.hpp and into main.cpp:
Files are 20K. Run make, output is 1 and files are 48K. Change int = 1 to int = 2.
make: output is 2 and files are 1.9M, nearly all of that in foo.hpp.gch. Change int = 2 to int = 3.
make: output is 3, foo.hpp.gch did update.

QUESTIONS
Why does make update a precompiled header (.gch) in one case, but not in the other? Why are the file sizes so different? What happened to the content of iostream? And how can I force make to always take into account changes in header files? I tried adding -fsyntax-only to CPPFLAGS, per this answer, but that gave an error.

user3100212
  • 101
  • 1
  • 5

1 Answers1

0

To partially answer my own question, in the Makefile I changed $^ to -o $@ $< and that seems to fix everything. The header files now update every time, and the combined files are only 144K. I got this idea here.

user3100212
  • 101
  • 1
  • 5