Home > Net >  Makefile - No rule to make target `PATH/TO/file.o`
Makefile - No rule to make target `PATH/TO/file.o`

Time:10-12

I'm really new to makefile and I'm struggling to make it compile all the .cpp files inside the src folder.

right now the makefile looks like this:

SRC_DIR = src
OBJ_DIR = build/obj
INCLUDE_DIR = $(SRC_DIR)/inc

SRC_DIRS = $(wildcard $(SRC_DIR) $(SRC_DIR)/* $(INCLUDE_DIR)/*)
C_FILES = $(filter %.cpp, $(wildcard $(SRC_DIRS)/*.cpp))
OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(notdir $(C_FILES:.cpp=.o)))

INCLUDE_PATH = -I$(INCLUDE_DIR)
CXXFLAGS = $(INCLUDE_PATH)
CXX = g  
CFLAGS = -Wall -g
CC = gcc

$(OBJ_DIR)/%.o: $(SRC_DIRS)/%.cpp

pile: $(OBJ_FILES)

test:
        @echo $(C_FILES)
        @echo $(OBJ_FILES)

and what I want to achieve with each rule is:

  • pile: when calling make pile it should check for all the .o files inside build/obj.
  • $(OBJ_DIR)/%.cpp: find the equivalent .cpp file inside the source folder and compile it.

I suppose that there's something wrong with my second rule, but I can't really tell why cause the test rule is printing all the files.. :/

thank you in advance for any help.

CodePudding user response:

Looks like SRC_DIRS could be an array which will not be happy with that matcher function. Try changing:

$(OBJ_DIR)/%.o: $(SRC_DIRS)/%.cpp

to

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

CodePudding user response:

There are many problems here but the most fundamental is that you are not actually defining a rule when you write this line:

$(OBJ_DIR)/%.o: $(SRC_DIRS)/%.cpp

A rule consists of targets, prerequisites and a recipe. Here you have not provided a recipe, so you haven't told make how to actually build anything.

The other issue is as mentioned by @Ryan-Miller; you can't use multiple directories in a single rule like this. But you can just avoid the complexity anyway if all your source files are in a single directory.

If that's not what you want please make your question clear as to ALL the different things you need. You wrote "$(OBJ_DIR)/%.cpp: find the equivalente .cpp file inside the source folder and compiles it". How exactly are you defining "the equivalent .cpp file"? Computer programming is all about the details!

  • Related