libsdl-org/SDL_ttf

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

mattmaniak opened this issue · 4 comments

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>
  1. Try to compile it via:
gcc ttf_included.h -I /opt/homebrew/Cellar/sdl2_ttf/2.22.0/include
  1. 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.

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

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.

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)

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