I am still new to programming in Linux with the GNU compilers and Makefiles. I'm trying to figure out how to compile all the header files and libraries with their respective object files.
At first this was my Makefile:
all:
g++ main.cpp
g++ main.o -o game -lsfml-graphics -lsfml-window -lsfml-system
The files in my directory where:
main.cpp Window.h Game.h Makefile
All the methods where in the their header files. I would run make and it would compile. Then I moved all the methods in to their own .cpp files, so my directory would look like this:
main.cpp Window.h Window.cpp Game.h Game.cpp Makefile
So then I changed my Makefile to:
CC = g++
CFLAGS = -std=c++11 -c -fpermissive
LDLIBS = -lsfml-graphics -lsfml-window -lsfml-system
SOURCES= main.cpp Window.cpp Game.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=game
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ $(LDLIBS)
.cpp.o:
$(CC) $(CFLAGS) $< -o $@
I run make and get this:
Window.cpp:105:3: error: ‘m_windowTitle’ was not declared in this scope
m_windowTitle, style);
^
Window.cpp: In member function ‘void Window::Destroy()’:
Window.cpp:110:2: error: ‘m_window’ was not declared in this scope
m_window.close();
^
Makefile:15: recipe for target 'Window.o' failed
make: *** [Window.o] Error 1
Only when I include the SFML Graphics.hpp file in Window.h and Game.h can I compile correctly.
Game.h:
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
Window.h:
#ifndef WINDOW_H
#define WINDOW_H
#include <SFML/Graphics.hpp>
Is that correct should I need to include the Graphics.hpp file in my other header files? And if so, why didn't I need to do it before?