synchronizer
synchronizer

Reputation: 2075

SDL Max OSX via Homebrew, Makefile not quite working

I have installed SDL through homebrew, and it works perfectly with my test program if I enter the following command directly in the terminal:

 g++ -O3 -g -Wall -Wextra -std=c++1y hello.cpp hello_main.cpp `sdl2-config --cflags --libs` -o hello

but unfortunately my attempts to write a makefile (I will definitely need one) have yielded unsuccessful/unexplained results. I am following this, but my configuration is different/I am not specifying Cocoa (I don't need to) so I expect that the issues I am encountering are probably due in part to my different requirements: Compiling SDL on OS X with makefile Example:

CC=g++
CFLAGS=-c -Wall
SDLFLAGS=`sdl-config --cflags --libs` -framework Cocoa
SOURCES=main.cpp Game.cpp IO.cpp Board.cpp Pieces.cpp Piece.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=tetris

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
 $(CC) $(OBJECTS) $(SDLFLAGS) -o $@

.cpp.o:
 $(CC) $(CFLAGS) $< -o $@

clean:
 rm -rf *.o $(EXECUTABLE)

My makefile so far:

CXX       = g++
CXXFLAGS  = -c -O3 -g -Wall -Wextra -std=c++1y
SDLFLAGS  = `sdl2-config --cflags --libs`
SOURCES   = hello_main.cpp hello.cpp 
OBJECTS   = $(SOURCES:.cpp=.o)
EXECNAME  = hello

all: $(SOURCES) $(EXECNAME)

$(EXECUTABLE): $(OBJECTS)
    $(CXX) $ (OBJECTS) $(SDLFLAGS) -o $@

.cpp.o:
    $(CXX) $(CXXFLAGS) $< -o $@

clean :
    -rm -f *.o *.core $(EXECNAME)

In my .hpp header file if I #include <SDL.h> and run the one-liner command, everything is successful. If I try my makefile above, cannot be found, but if I then change the directive into #include <SDL2/SDL.h> the library is discovered. Yet the console output is the following:

g++ -c -O3 -g -Wall -Wextra -std=c++1y    hello.cpp   -o hello

which is odd.

Running ./hello yields a "permission denied" error, which confirms that the linking and compilation were not successful. Everyone's system is a little bit different and the questions I've found so far don't help in this case. I am very close to having this working (but then again, how would I start using this in an IDE? I suppose that as long as I can import the fixed makefile or build from the terminal/edit only from the IDE, I am fine.)

What changes in the makefile do I need to make? Thank you.

EDIT:

Variation 1:

CXX         = g++
CXXFLAGS    = -O3 -g -Wall -Wextra -std=c++1y -c
SDLCFLAGS   = `sdl2-config --cflags`
SDLLIBFLAGS = `sdl2-config --libs`

SOURCES   = hello_main.cpp hello.cpp 
OBJECTS   = $(SOURCES:.cpp=.o)
EXECNAME  = hello

all: $(SOURCES) $(EXECNAME)

$(EXECUTABLE): $(OBJECTS)
    $(CXX) $ (OBJECTS) $(SDLLIBFLAGS) -o $@

.cpp.o:
$(CXX) $(CXXFLAGS) $(SDLCFLAGS) $< -o $@

clean :
    -rm -f *.o *.core $(EXECNAME)

Upvotes: 0

Views: 324

Answers (2)

synchronizer
synchronizer

Reputation: 2075

I chatted with a friend and figured what was wrong: a bunch of typos and rule oddities. The following works, for anyone out there who needs a basic makefile:

CXX         = g++
CXXFLAGS    = -O3 -g -Wall -Wextra -std=c++1y
#LDFLAGS     = -lSDL2_image
SDLCFLAGS   = $(shell sdl2-config --cflags)
SDLLIBFLAGS = $(shell sdl2-config --libs)

SOURCES   = hello_main.cpp hello.cpp 
OBJECTS   = $(SOURCES:.cpp=.o)
EXECNAME  = hello

all: $(EXECNAME)

$(EXECNAME): $(OBJECTS)
    $(CXX) $(OBJECTS) $(SDLLIBFLAGS) $(LDFLAGS) -o $@

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

clean :
    -rm -f *.o *.core $(EXECNAME)

Upvotes: 1

Florian Zwoch
Florian Zwoch

Reputation: 7393

You should split your sdl2-config into two - as there are two steps. sdl2-config --cflags should go in the compiler step - thats is the .cpp:.o line in your example. The linking step should be sdl2-config --libs then. The second one seems fine for your case, the additional --cflags there does no harm but is not required.

Upvotes: 0

Related Questions