Sergio Porres
Sergio Porres

Reputation: 67

SDL_ttf cannot find "SDL.h", but main.cpp can

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.

Upvotes: 3

Views: 6145

Answers (3)

OctaviaLo
OctaviaLo

Reputation: 1396

I had installed SDL via homebrew and the ttf library was including "SDL.h", which the linker could not find due to the file structure of the homebrew installation.

There are two parts to this - one is to get the compiler happy when it complains the header "SDL.h" cannot be found, and the other to get the linker to link to the library. SDL.h contains the definitions used in the programme, and these definitions are referenced in the .dylib file. In the terminal you can do nm libSDL2_ttf-2.0.0.dylib and see where in memory the commands are allocated.

What I had to do to get my programme running was to provide the compiler the paths for all the folders in which "SDL.h" could be found. This satisfied the compiler as "SDL.h" included in the SDL_ttf.h file could not be found due to how homebrew installs SDL. I also had to link to the .dylib file in the compiler options.

My build file below.

The commands -lSDL2_ttf tells the the linker ld to link to the .dylib files in /opt/homebrew/Cellar/sdl2_ttf/2.20.2/lib

#!/bin/bash

set -e

INCLUDES="-I/opt/homebrew/Cellar/sdl2/2.28.3/include/ -I/opt/homebrew/Cellar/sdl2/2.28.3/include/SDL2/ -I/opt/homebrew/Cellar/sdl2_ttf/2.20.2/include/"

LIBS="-L/opt/homebrew/Cellar/sdl2/2.28.3/lib/ -L/opt/homebrew/Cellar/sdl2_ttf/2.20.2/lib/ -lSDL2 -lSDL2_ttf"

gcc -Wall -o main main.c ${INCLUDES} ${LIBS}

To get it running in CLion, you can put this in the compiler options: -Wall -I/opt/homebrew/Cellar/sdl2/2.28.3/include/ -I/opt/homebrew/Cellar/sdl2/2.28.3/include/SDL2/ -L/opt/homebrew/Cellar/sdl2/2.28.3/lib/ -I/opt/homebrew/Cellar/sdl2_ttf/2.20.2/include/ -L/opt/homebrew/Cellar/sdl2_ttf/2.20.2/lib/ -lSDL2 -lSDL2_ttf

Upvotes: 0

Sergio Porres
Sergio Porres

Reputation: 67

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 $@

Upvotes: 0

LordAro
LordAro

Reputation: 1289

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

Upvotes: 0

Related Questions