libsdl-org / SDL_ttf

Support for TrueType (.ttf) font files with Simple Directmedia Layer.
zlib License
370 stars 124 forks source link

macOS with Brew - "fatal error: 'SDL.h' file not found" when including <SDL2/SDL_ttf.h> as a header #361

Closed mattmaniak closed 3 months ago

mattmaniak commented 3 months ago

I am unable to compile a code with SDL2_ttf (2.22.0) header included. It is installed via Brew. SDL2 version is 2.30.3.

Repro steps:

  1. Ensure you have Brew installed (https://brew.sh).
  2. Install SDL2 2.30.3 via Brew: brew install sdl2@2.30.3.
  3. Instal SDL2_ttf 2.20.0 via Brew: brew install sdl2_ttf@2.22.0.
  4. Create an example header file that imports SDL TTF lib, eg. ttf_included.h:
    #include <SDL2/SDL_ttf.h>
  5. Try to compile it via:
    gcc ttf_included.h -I /opt/homebrew/Cellar/sdl2_ttf/2.22.0/include
  6. The outcome is an error:
    In file included from ttf_included.h:1:
    /opt/homebrew/Cellar/sdl2_ttf/2.22.0/include/SDL2/SDL_ttf.h:39:10: fatal error: 'SDL.h' file not found
    #include "SDL.h"
         ^~~~~~~
    1 error generated.
madebr commented 3 months ago

SDL2_ttf's headers depend on SDL2's headers, so you need to add the include path of SDL2 as well.

mattmaniak commented 3 months ago

Changing the source file to

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

and command to

gcc ttf_included.h -I /opt/homebrew/Cellar/sdl2/2.30.3/include -I /opt/homebrew/Cellar/sdl2_ttf/2.22.0/include

produces the same issue.

madebr commented 3 months ago

All SDL2 satellite libraries include SDL2 headersas "SDL.h", which means you need to add the path that contains SDL.h. In your case: -I /opt/homebrew/Cellar/sdl2/2.30.3/include/SDL2

If you get homebrew pkg-config (or pkgconf) working, you can do the following instead:

gcc ttf_included.h $(pkg-config --cflags --libs sdl2 SDL2_ttf)
mattmaniak commented 3 months ago

Thanks! -I /opt/homebrew/Cellar/sdl2/2.30.3/include/SDL2 did the job.