Home > Blockchain >  after Adding header file to dependency, makefile didn't work for main.cpp
after Adding header file to dependency, makefile didn't work for main.cpp

Time:03-27

CPPFLAGS = -std=c  11

SRC_DIR    := src
HEADER_DIR := include
BIN_DIR    := bin
OBJ_DIR     := $(BIN_DIR)/obj

EXECUTABLE := $(BIN_DIR)/main

OBJECTS = $(addprefix $(OBJ_DIR)/,main.o admin.o number.o SHA256.o signatures.o user.o)


all: $(EXECUTABLE)

directories:
    mkdir $(OBJ_DIR)

$(EXECUTABLE): $(OBJECTS)
    g   -o $@ $(CPPFLAGS) $(OBJECTS) 
    
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADER_DIR)/%.h
    g   $(CPPFLAGS) -c $< -o $@ -I $(HEADER_DIR)


clean: 
    del $(OBJ_DIR)\*.o $(EXECUTABLE)
    CPPFLAGS = -std=c  11

Above is my makefile. It doesn't update after main.cpp gets changed while it works for main.cpp only when I remove the header file from the dependency. i.e. change this line $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADER_DIR)/%.h to $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp Why is that happening?

CodePudding user response:

This rule:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADER_DIR)/%.h

tells make how to build an object file if and only if it can find an appropriately named .cpp file and an appropriately named .h file. If either of those files cannot be found, and make can't find a rule to build them, then this rule doesn't match and make will continue looking for some other rule to build the object file.

If no other rule is found, make will tell you that there is no rule to build the object file.

If you're getting this error for main.cpp, then it means that there is no corresponding main.h file.

You either need to create a main.h file, or change this pattern rule to not put the header as a prerequisite (you add the headers of other files as prerequisites directly), or create a new pattern rule that doesn't require the header.

  • Related