5
votes

I'm trying to setup SDL2 to use with g++, a text editor and terminal. I have my SDL2.framework in /Library/Frameworks. I have a folder on my desktop named testsdl which contains two files:

1) main.cpp

2) makefile

when I type make I get the following error:main.cpp:2:10: fatal error: 'SDL2/SDL.h' file not found. here is a copy of my makefile

CXX = g++
SDL = -framework SDL2

CXXFLAGS = -Wall -c -std=c++11 -I ~/Library/Frameworks/SDL2.framework/Headers
LDFLAGS = $(SDL) -F /Library/Frameworks -F ~/Library/Frameworks/
EXE = SDL_Lesson0

all: $(EXE)

$(EXE): main.o
    $(CXX) $(LDFLAGS) $< -o $@

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

clean:
    rm *.o && rm $(EXE)

and here is a copy of main.cpp:

#include <iostream>
#include <SDL2/SDL.h>

int main(int, char**)
{
  if (SDL_Init(SDL_INIT_VIDEO) != 0)
  {
    std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
    return 1;
  }
  SDL_Quit();
  return 0;
}

I tried changing the #include to "SDL2/SDL.h" or just or any other possible combination. I had no problem setting it up through Xcode but can't figure out how to do it without using an IDE.

a second part of the question is: if I wanted to include the frameworks in the project folder itself so I can later distribute the binaries to people who do not have SDL on their machines how would I do that? thanks.

2
don't know the answer for sure but these things look suspicious to me: (1) why is there a space between -I and path ~/Library/Frameworks/SDL2.framework/Headers? Normally there is not a space between -I, -L, -l, or any other compiler flags and their parameters. (2) Does ~/Library/Frameworks/SDL2.framework/Headers/SDL2/SDL.h actually exist? If not then there is your problem. (3) If you want the compilation to be cross-platform (i.e. work on linux / bsd / windows) then I guess you should stay away from -framework also and use -L -l instead - Chris Beck
thanks, I tried removing the spaces but that did not help. The framework and the headers are all there - Benny Abramovici

2 Answers

2
votes

You were on the right track, but -F /Library/Frameworks needs to also be in the CXXFLAGS. The resulting commands should look something like:

g++ -Wall -F /Library/Frameworks -c -o main.o main.cpp
g++ main.o -o main -framework SDL2 -I /Library/Frameworks/SDL2.framework/Headers

Here's a simplified makefile that works for me on OSX 10.12.6:

CXX = g++

CXXFLAGS = -Wall -F /Library/Frameworks
LDFLAGS = -framework SDL2 -F /Library/Frameworks -I /Library/Frameworks/SDL2.framework/Headers

all: main

main: main.o
    $(CXX) main.o -o main $(LDFLAGS)

obj/main.o : main.cpp
    $(CXX) $(CXXFLAGS) -c main.cpp -o main.o

clean:
    rm main.o main
0
votes

if you go into

/Users/path/where/i/unzipped/SDL2-2.0.5

and run

sdl2-config --cflags --libs

it prints out the header include path and library path SDL

The full generic build probably looks something like:

g++ -std=c++11 main.cpp -o main.exe `sdl2-config --cflags` `sdl2-config --libs`

which you can translate into your make file if you like