Skip to content

Makefile does not rebuild when a header under src/ changes #15

Description

@dmccoystephenson

The Makefile lists only .cpp files as prerequisites of the executable, so a change to a header under src/ is not noticed by make. Facts read directly from the Makefile:

SOURCES = $(wildcard src/*.cpp)
...
$(TARGET): $(SOURCES)

No .h or .hpp file is part of the prerequisite list, and no dependency information is generated by the compiler invocation (g++ $(CXXFLAGS) $(SOURCES) -o $(TARGET)).

Reproduction, performed on master at f6d4369 with a scratch header that was removed afterwards:

  1. src/greeting.h was created containing #define GREETING "Hello World!", and src/testing.cpp was changed to #include "greeting.h" and to pass GREETING to log.
  2. make run compiled and printed [LOG] Hello World! as expected.
  3. Only the header was then edited, to #define GREETING "Hello from the header!".
  4. make run printed no Compiling line and ran the previous executable:
---
Running testing
./testing
[LOG] Hello World!
[DEBUG] debugFlag is true, so this message is shown.
Finished running testing

The stale output is the consequence: a project generated from this template that adds a header (the normal way a C++ project grows past one file) will run an out-of-date executable after a header-only edit, and make run will report nothing wrong. README.md reinforces the gap by describing only .cpp handling in the Repository layout table ("Any .cpp file added here is compiled.").

Suggested resolution, in keeping with the template's single-target build: add a HEADERS = $(wildcard src/*.h src/*.hpp) variable and list it alongside $(SOURCES) as a prerequisite of $(TARGET), then note in README.md that header changes trigger a rebuild. Full per-object dependency tracking (-MMD -MP) is deliberately not suggested, since it would require moving to per-object compilation and is heavier than a starter template needs.

This issue was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions