简体   繁体   English

如何在Makefile中正确使用变量?

[英]How to use variables properly in Makefile?

I was following a tutorial for creating a simple pong game and wanted to try using makefiles. 我正在按照教程创建一个简单的乒乓球游戏,并想尝试使用makefile。 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: 这是我的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时,这就是我得到的:

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. SFML似乎以前工作,所以我不认为这是问题所在。
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. 第一个是一个规则,它需要创建像./objstdafx.o这样的文件。 The second one expects files like ./obj/stdafx.o . 第二个需要像./obj/stdafx.o这样的文件。 So with the first one, make does not know how to build obj/stdafx.o . 所以对于第一个,make不知道如何构建obj/stdafx.o

See the documentation for more information about pattern matching rules. 有关模式匹配规则的更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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