0

I am new to makefiles and want to save all my object files in an own directory. I googled a lot and came to that solution:

CXX = clang++

# compiler flags
CXXFLAGS = -O3 -Wall -Werror -std=c++11
CFLAGS_SFML = -lsfml-graphics -lsfml-window -lsfml-system
SRCS = getInput.cpp createOutput.cpp main.cpp
OBJDIR = obj
OBJS = $(addprefix $(OBJDIR)/, SRCS:.cpp=.o)

all: program.exe

program.exe: $(OBJS)
    $(CXX) $(CXXFLAGS) -o program.exe $(OBJS) $(CFLAGS_SFML)

$(OBJDIR)/%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

When I try to run the makefile I get this error:

makefile:12: *** target pattern contains no `%'. Stop.

It seems as this error is quite common and does not tell the detail about what is wrong. It would be great if someone could help me.

Natjo
  • 2,005
  • 29
  • 75

1 Answers1

1

Problem with OBJS = $(addprefix $(OBJDIR)/, SRCS:.cpp=.o)

Try this one. First you have to make the obj directory also.

CXX = clang++

# compiler flags
CXXFLAGS = -O3 -Wall -Werror -std=c++11
CFLAGS_SFML = -lsfml-graphics -lsfml-window -lsfml-system
SRCS = main.cpp
OBJDIR = obj
OBJS = $(addprefix $(OBJDIR)/, $(SRCS:.cpp=.o))


all: program.exe

program.exe: $(OBJS)
    $(CXX) $(CXXFLAGS) -o program.exe $(OBJS) $(CFLAGS_SFML)

$(OBJDIR)/%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

For more information use this.

Community
  • 1
  • 1
sanjay
  • 735
  • 1
  • 5
  • 23
  • Thank you, that solved my problem. great. Just one last thing, it complains the directory "obj" does not exist. So when I add it manually everything works fine. But is there a more elegant solution? – Natjo Jan 05 '16 at 12:46
  • @Jonas i am not expert in makefile. You can refer to this book http://www.oreilly.com/openbook/make3/book/index.csp. Mark as solution and upvote :P – sanjay Jan 05 '16 at 12:48
  • I will suggest you to use some IDE or buil tool like CMake. Because it's better to spend time on code rather build system – sanjay Jan 05 '16 at 13:00