SDL in C has a problem with SDL_ttf.h not finding SDL.h

Problem description:

My student made a C app using SDL graphics (SDL2 and a SDL2_ttf font) and I would like to add it to the documentation to help other repliters trying to get SDL running in C or C++. I think there’s a need for this as SDL is a very popular 2D video game development platform, among other things, and the existing documentation search results on “SDL” strongly imply that it’s very difficult if not impossible to use SDL on Replit with C/C++; probably in part because of this bug:

Please see the Makefile in that repl. For some reason, the <SSL2/SDL_ttf.h> include file (from adding and pkgs.SDL2_ttf to replit.nix) tries to #include "SDL.h", which it can’t find without the very lengthy
compiler flag, which was very difficult to find (using clang -v -E -x c - < /dev/null 2>&1 | tr ' ' '\n' | grep 'SDL2-.*-dev/include' in the shell.)

How can I work with Replit staff to make sure <SDL2/SDL_ttf.h> can find "SDL.h"?

Expected behavior:

#include "SDL.h" in <SDL2/SDL_ttf.h> should succeed without the hardcoded CFLAGS
-I/nix/store/...-SDL2-2.24.2-dev/include/SDL2 flag in the Makefile.

Actual behavior:

In file included from ./view.c:22:
/nix/store/29r5cssghd3i1d9h9lmyr3hzgb157zvb-SDL2_ttf-2.20.1/include/SDL2/SDL_ttf.h:39:10: fatal error: 'SDL.h' file not found
#include "SDL.h"

Steps to reproduce:

Fork and remove the
-I/nix/store/.../include/SDL2 flag from the CFLAGS in the Makefile.

1 Like works without using hardcoded paths. SDL is packaged weirdly by Nix so that it can be reproducible (by enabling it to work the same alongside other SDL versions available on Nix). More: Curl Library Installation - #5 by UMARismyname


Thank you, Umar. I tried this in replit.nix:

    env = {
      CPATH = lib.makeSearchPath "include/SDL2" pkgs.SDL_ttf;

Unfortunately, I get: error: undefined variable 'lib'

Any idea why I don’t have the string functions lib?

lib is part of the pkgs set. I used with but you could alternatively prefix lib with pkgs.. Also you might need to see my .replit and Makefile

1 Like

[For the benefit of future searchers, there’s a template at enjoy!]

I got it, this replit.nix fixed everything:

{ pkgs }: {
	deps = [
	env.CPATH = pkgs.lib.makeSearchPath "include/SDL2" [ ];
	env.CFLAGS = "-lSDL2 -lSDL2_ttf";

Accordingly, it was not necessary to add the -lSDL2 -lSDL2_ttf CFLAGS in the Makefile.

Can we get that env setting folded into the SDL2_* pkgs somehow?

1 Like

you can use nested setsenv.CPATH =

future note: don’t use this for env.PYTHON_LD_LIBRARY_PATH or other attributes which may be programmatically modified, e.g. by nix-editor

1 Like

That works. Now, is there any way to get that env.CPATH = ... setting included in pkgs.SDL2_ttf and the other SDL2_* pkgs?

not sure what you mean

Is there a way (for Replit admins?) to change the SDL2_ttf package so that adding it to the deps will automatically add the include/SDL2 path to the CPATH env variable without the user having to explicitly specify it?

Can the CFLAGS similarly be set merely by adding the pkgs deps?

I suppose they could add a Nix module (.replit config) or similar for it. And it would be possible for replit’s nix function which references replit.nix to take in a different [1] attribute for include packages and has an index of the working include folders needed by well-known multi-versioned packages like SDL.

  1. implementing in the existing deps would break compatibility with Nix’s reproducible system ↩︎

1 Like

Can Replit add settings to the official version of the pkgs? If so, who and/or where would I ask for that?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.