I created a makefile based on the GNU Make the tutorial: https://www.gnu.org/savannah-checkouts/gnu/make/manual/html_node/index.html#SEC_Contents.
The make file works fine on initial make, but if a file is changed and make is ran there is a circular dependency dropped message and it does not build the changed file. The dropped dependency is bin/main.o <-bin/main.o. This makes sense as main.o should not depend on main.o. I searched several sites, including stackoverflow and found some useful answers, but I have not been able to resolve my issue. These two links were the most relevant to my issue: Makefile export .o file to a different path than .cpp and Makefile circular dependency error
Based on the above information I edited my makefile and at least now I can debug easier. The dependency issue probably has to do with the subsequent target and prerequisite after wildcard expansion, but I am just not seeing it.
Here is my makefile:
#For compilation
CC=g++
CC_FLAGS= -g -Wall -Werror -ansi -pedantic
#make[1]: main.o all <- all
#Folder structure
BIN_DIR:=bin
SRC_DIR:=src
H_DIR:=src/header
all:rshell
#VPATH = $(SRC_DIR)
H_FILES:=$(wildcard $(H_DIR)/*.h)
DEPS:$(wildcard $(BIN_DIR)/*.d)
CPP_FILES:=$(wildcard $(SRC_DIR)/*.cpp)
OBJECTS := $(CPP_FILES:$(SRC_DIR)/%.cpp=$(BIN_DIR)/%.o)
$(BIN_DIR)/%.o: $(SRC_DIR)/%.cpp $(H_DIR)/%.h
mkdir -p $(BIN_DIR)
$(CC) -I$(H_DIR) -o $@ -c $< $(CC_FLAGS)
$(BIN_DIR)/main.o:$(SRC_DIR)/main.cpp $(OBJECTS) $(H_FILES)
$(CC) -I$(H_DIR) -o $@ -c $(SRC_DIR)/main.cpp $(CC_FLAGS)
rshell: $(OBJECTS)
$(CC) -o $(BIN_DIR)/$@ $(OBJECTS) $(CC_FLAGS)
include $(DEPS)
$(MAKEFILE_LIST): ;
%:: %,v
%:: RCS/%,v
%:: RCS/%
%:: s.%
%:: SCCS/s.%
.PHONY: clean
clean:
rm -rf $(BIN_DIR)
Question1: If the wildcard expansion is causing the circular dependency, how could I fix?
Question2: How does one trace wildcard expansion? Would it be correct to echo the names of the files and trace that way? Running make -d was useful, but I am not seeing how to avoid it. My assumption is these lines are causing the circular dependencies:
$(BIN_DIR)/main.o:$(SRC_DIR)/main.cpp $(OBJECTS) $(H_FILES)
$(CC) -I$(H_DIR) -o $@ -c $(SRC_DIR)/main.cpp $(CC_FLAGS)
Thank you for your help and insight.