Search code examples
c++macosmakefilesdl-2

setup SDL2 mac through command line


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 <sdl.h> 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?


Solution

  • 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