简体   繁体   中英

How to use variables properly in Makefile?

I was following a tutorial for creating a simple pong game and wanted to try using makefiles. I did some research and wrote one, but i can't get it to work.
This is how my project folder looks like.

project/
|__include/
|  |__*.h
|__release/
|  |__obj/
|  |__Makefile
|__src/
   |__*.cpp

This is my Makefile:

CXX         :=  g++

# Directories
SDIR        :=  ../src
IDIR        :=  ../include
ODIR        :=  ./obj
VPATH       :=  $(SDIR)

# Files
_SRCS       :=  stdafx.cpp Pang.cpp Game.cpp MainMenu.cpp SplashScreen.cpp
SRCS        :=  $(patsubst %,$(SDIR)/%,$(_SRCS))
_DEPS       :=  stdafx.h Game.h MainMenu.h SplashScreen.h
DEPS        :=  $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJS       :=  $(_SRCS:.cpp=.o)
OBJS        :=  $(patsubst %,$(ODIR)/%,$(_OBJS))
EXES        :=  Pang

# Parameters
CXXFLAGS    :=  -O2 -g -Wall -fmessage-length=0
LIBS        :=  -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system

# Default rule
all: $(SRCS) $(EXES)

# Generic compilation rule
$(ODIR)%.o : %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

# Link object files
$(EXES): $(OBJS)
    $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)

# Clean Rule
clean:
    rm -f $(ODIR)/*.o $(EXES)

When i run make, this is what i get:

make all 
make: *** No rule to make target `obj/stdafx.o', needed by `Pang'.  Stop.

The SFML seemed to be working before, so i don't think it is the problem.
Any help would be great!

this:

$(ODIR)%.o : %.cpp

should be:

$(ODIR)/%.o : %.cpp

(note the slash)

The first one is a rule which expects files like ./objstdafx.o to be created. The second one expects files like ./obj/stdafx.o . So with the first one, make does not know how to build obj/stdafx.o .

See the documentation for more information about pattern matching rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM