0

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?

user657267
  • 20,568
  • 5
  • 58
  • 77
Marco Lugo
  • 169
  • 2
  • 10
  • 1
    It's impossible to give you an answer because you haven't provided a [mvce]. As a guess I'd say you were originally including the lib headers in `main.cpp` before `window.h`, so the definitions were available. In any case this has nothing to do with make. – user657267 Nov 07 '16 at 05:54
  • You should use `CXX=g++` and `CXXFLAGS`; BTW you are not (and should not) compiling header files. See [this](http://stackoverflow.com/a/14180540/841108) – Basile Starynkevitch Nov 07 '16 at 05:59
  • It looks like you are not familiar with C++11 (and the role of the preprocessor). So take time to read http://stroustrup.com/Programming/ – Basile Starynkevitch Nov 07 '16 at 06:10
  • Thanks for your help, I'll take a look at the links you've sent – Marco Lugo Nov 07 '16 at 23:09

0 Answers0