Reputation: 855
This is my project directory file tree:
.
├── entity.cpp
├── entity.hpp
├── entitytypes.hpp
├── main.cpp
├── Makefile
├── player.cpp
└── player.hpp
Makefile
's contents is this:
CC=g++
CFLAGS=-Wall -pg
LDFLAGS=-lsfml-graphics -lsfml-window -lsfml-system
EXE=gametest
all: $(EXE)
%.o: %.c
$(CC) -c $< -o $@ $(CFLAGS)
$(EXE): main.o entity.o player.o
g++ $^ -o $@ $(LDFLAGS)
And very oddly, this is the output when I type make
:
g++ -c -o main.o main.cpp
g++ -c -o entity.o entity.cpp
g++ -c -o player.o player.cpp
g++ main.o entity.o player.o -o gametest -lsfml-graphics -lsfml-window -lsfml-system
So as you can see, the last line of the output corresponds to the $(EXE)
section of the Makefile. It's linking the program with the correct flags, etc. My problem is that when each individual object file is build, it's not at all taking into account my %.o: %.c
rule. This is clear since when it builds the objects, there is a long gap between g++
and -c
, which I didn't write. Also, it's missing out my $(CFLAGS). Even weirder, when I comment out the %.o: %.c
section, the Makefile does the exact same thing.
It seems to me like it's using some kind of default build command, and just ignoring mine.
I've looked at Makefiles I've written in the past which as far as I can tell use the exact same macros in the same way, so I'm very confused as to why this is happening.
Upvotes: 1
Views: 516
Reputation: 30781
The built-in %.o: %.cpp
rule uses CXX
(not CC
) to specify the compiler, and CXXFLAGS
(not CFLAGS
) for the flags. So re-write your Makefile to set those variables, and you don't need to specify the pattern rule yourself:
CXX = g++
CXXFLAGS = -Wall -Wextra -pg
LIBS = -lsfml-graphics -lsfml-window -lsfml-system
gametest: main.o entity.o player.o
gametest: LINK.o = LINK.cc
Upvotes: 1
Reputation: 48615
It looks like a half converted Makefile for C
. It uses the wrong variables and looks for C
files rather than C++
. Try this (untested):
CXX := g++
CXXFLAGS := -Wall -pg
LDFLAGS := -lsfml-graphics -lsfml-window -lsfml-system
EXE := gametest
all: $(EXE)
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) -o $@ $<
$(EXE): main.o entity.o player.o
$(CXX) -o $@ $^ $(LDFLAGS)
C++
uses CXX
whereas C
uses CC
, same with CXXFLAGS
.
Note: Remember the indentations are TAB
s not spaces!
Upvotes: 2
Reputation: 7211
You named your files as *.cpp while your rules said *.c, that's why.
Upvotes: 3