0

I wrote makefile which seems to works fine except for one case: When I change the makefile itself it does not recompile all files.

I've done all those steps:

  1. make clean
  2. make all
  3. make all (it returns "Nothing to be done for all") - ok
  4. change makefile (add empty line etc.)
  5. make all (still it returns "Nothing to be done for all" - not ok at all

In my opinion, it should recompile when the makefile file was changed.

What can be improved to force makefile to recompile all files when I make modifications in makefile?

Makefile:

################################### VARIABLES ############################################
# Output settings:
BUILD_DIR := ./build
BIN_DIR := $(BUILD_DIR)/bin

TARGET_FOLDER := $(BUILD_DIR)
TARGET_NAME := app.exe

# Input settings:
SOURCE_DIR = ./
SOURCES := $(SOURCE_DIR)/main.cpp

INCLUDES := $(SOURCE_DIR)

# Compilator settings:
CC := gcc
CXX := g++

# Compilation flags:
COMMON_FLAGS := -Wall -Werror

CFLAGS := $(COMMON_FLAGS)

CXXFLAGS :=  $(COMMON_FLAGS)

LDFLAGS :=

# VERBOSE OPTION
VERBOSE ?= 0 # Set to zero to silent makefile output
ifeq ($(VERBOSE),1)
  NO_ECHO :=
else
  NO_ECHO := @
endif

############################### MAGID PART START #########################################
TARGET := $(abspath $(TARGET_FOLDER)/$(TARGET_NAME))

# Remove double slash (//) from path, it can make issue with vpath.
# Just use abspath :)
SOURCES_ABS := $(abspath $(SOURCES) )
INCLUDES_ABS := $(abspath $(INCLUDES) )

SOURCES_C := $(filter %.c, $(SOURCES_ABS) )
SOURCES_CC := $(filter %.cc, $(SOURCES_ABS) )
SOURCES_CPP := $(filter %.cpp, $(SOURCES_ABS) )

OBJECTS_LIST := $(notdir $(patsubst %.c, %.o, $(SOURCES_C) ) )
OBJECTS_LIST += $(notdir $(patsubst %.cc, %.o, $(SOURCES_CC) ) )
OBJECTS_LIST += $(notdir $(patsubst %.cpp, %.o, $(SOURCES_CPP) ) )

OBJECTS :=  $(addprefix $(BIN_DIR)/, $(OBJECTS_LIST) )
DEPENDENCIES := $(patsubst %.o, %.d, $(OBJECTS) )
INCLUDES_FLAG := $(addprefix -I, $(INCLUDES_ABS) )

COMMON_FLAGS += $(INCLUDES_FLAG)
COMMON_FLAGS += -MMD -MP    # Generate dependency files

CFLAGS += $(COMMON_FLAGS)
CXXFLAGS += $(COMMON_FLAGS)

# Use vpath to search sources:
VPATH += $(sort $(dir $(SOURCES_C) ) )
VPATH += $(sort $(dir $(SOURCES_CC) ) )
VPATH += $(sort $(dir $(SOURCES_CPP) ) )

all: app

app: $(TARGET)

$(TARGET): $(OBJECTS) | $(BUILD_DIR)
    $(info Preparing: $@)
    $(NO_ECHO) $(CXX) $(CXXFLAGS) -o $@ $^

$(BIN_DIR)/%.o : %.c | $(BIN_DIR)
    $(info Preparing: $@)
    $(NO_ECHO) $(CC) -c $(CFLAGS) $< -o $@

$(BIN_DIR)/%.o : %.cc | $(BIN_DIR)
    $(info Preparing: $@)
    $(NO_ECHO) $(CXX) -c $(CXXFLAGS) $< -o $@

$(BIN_DIR)/%.o : %.cpp | $(BIN_DIR)
    $(info Preparing: $@)
    $(NO_ECHO) $(CXX) -c $(CXXFLAGS) $< -o $@

$(BIN_DIR): |$(BUILD_DIR)
    $(NO_ECHO) -mkdir $@

$(BUILD_DIR):
    $(NO_ECHO) -mkdir $@

.PHONY : clean

clean:
    -rm -fr $(TARGET) $(BUILD_DIR) *.o *.d

-include $(DEPENDENCIES)

main.cpp

#include <iostream>

using namespace std;

int main(void)
{
    cout<<"HA HA"<<endl;

    return 0;
}
adixmasz
  • 1
  • 1
  • 3
    There's nothing I can see in the `Makefile` that make it depend on itself to force a rebuild. – Some programmer dude Aug 31 '21 at 10:44
  • 2
    Do you have `Makefile` itself in dependencies? See https://stackoverflow.com/q/29877503/580083. – Daniel Langr Aug 31 '21 at 10:48
  • 2
    Does this answer your question? [making all rules depend on the Makefile itself](https://stackoverflow.com/questions/3871444/making-all-rules-depend-on-the-makefile-itself) – Andreas Aug 31 '21 at 10:54
  • @Andreas It looks nice, but We use GNU Make 4.2.1. I am sure that it can be done differently because I have project with makefile where its works as I want. I just can not find the place where makefile itself is add as dependecy. – adixmasz Aug 31 '21 at 11:16
  • 1
    There are more answers in the duplicate provided by @Andreas. Have you checked them as well? – Some programmer dude Aug 31 '21 at 11:20
  • As a side note, I resort to `make -B` whenever I change the `makefile`, or any files included by the `makefile`. Works for me. Compiler cache makes it fast enough. Would not recommend being weird about it. That weirdness is better spent migrating to different build system. Feel free to learn the hard way. Wish I hadn't. But maybe your case is different. – Andreas Aug 31 '21 at 12:47

0 Answers0