SDL_ttf cannot find “SDL.h”, but main.cpp can

2019-07-23 17:32发布

问题:

I am writing a make file to compile a very simple SDL2 program.

So far it compiles SDL2 just fine, and now I am working on compiling the extension frameworks SDL2_image and SDL_ttf.

It seems that MAKE is finding the SDL_ttf.h properly, but then SDL_ttf.h cant find "SDL2/SDL.h".

Here is the error:

In file included from main.cpp:3:
/Library/Frameworks/SDL2_ttf.framework/Headers/SDL_ttf.h:30:10: fatal error:
  'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
         ^
1 error generated.
make: *** [main.o] Error 1

Note, when I included SDL2 like this:

#include <SDL2/SDL.h>

I could not compile even a basic program (SDL2 with no extensions). I got it to work by changing it to this:

#include "SDL.h"

(I also heard the latter syntax is more correct for portability?)

Any ideas?

I am doing this on OS X Mavericks

Here are my files: main.cpp

#include <iostream>
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"

int main(int argc, char * arg[])
{
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
         std::cout << "ERROR" <<std::endl;

        return -1;
    }

    SDL_Window * window = SDL_CreateWindow("Name", SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    640,
    480,
    SDL_WINDOW_OPENGL);
     if (TTF_Init() == -1)
     {
     sdl::cout << "SDL_ttf failed" << std::endl;
     }

    SDL_Surface* tempSurface = IMG_Load("test.png");

    if (tempSurface == nullptr)
    {
    std::cout << "failed to load test.png" << std::endl;
    }

    SDL_FreeSurface(tempSurface);

    SDL_Delay(5999);

    SDL_DestroyWindow(window);

    SDL_Quit();

    return 0;

}

makefile

CXX = clang++
SDL = -framework SDL2 -framework SDL2_ttf -framework SDL2_image

CXXFLAGS = -Wall -c -std=c++11 -I /Library/Frameworks/SDL2.framework/Headers -I       /Library/Frameworks/SDL2_ttf.framework/Headers -I    /Library/Frameworks/SDL2_image.framework/Header

LDFLAGS = $(SDL) -F /Library/Frameworks
EXE = test

all: $(EXE)

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

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

Edit:

I have SDL2 and SDL2_ttf installed into the /Library/Frameworks folder per the instructions in the DMG files.

回答1:

I believe you need SDL2_ttf to go with SDL2.

SDL_ttf (2.0.11) needs SDL (1.2)

and SDL2_ttf (2.0.12) needs SDL2



回答2:

I had installed the SDL2 libraries via DMG, which is the correct way to make them available to Xcode.

However, I want to use g++ and compile via MAKE which led to the problems I detailed above.

The answer is to install SDL2 and the extension libraries UNIX style as detailed here http://www.ginkgobitter.org/sdl/?FAQMacOSX

I was then able to use include statements like this:

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"

My makefile now looks like this (note use of sdl2-config and how TTF and image libraries are linked):

CXX = clang++
SDL = `sdl2-config --libs`

CXXFLAGS = -Wall -c -std=c++11 `sdl2-config --cflags`
LDFLAGS = $(SDL) -lSDL2_ttf -lSDL2_image 
EXE = test

all: $(EXE)

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