SDL2; "SDL.h" not found

Hi. I have similar issue to this one
When i try to use #include <SDL2/SDL_image.h> i’m having en error on compilation due to this file trying to include #include "SDL.h":

make
clang -c src/cleanup.c -o obj/cleanup.o -std=c11 -Wall -Wextra -Werror -Wpedantic  -Iinc 
In file included from src/cleanup.c:1:
In file included from src/../inc/gamelib.h:5:
/nix/store/j9ar69c160x3lbj94nz127l536l9s7h8-SDL2_image-2.8.2/include/SDL2/SDL_image.h:32:10: fatal error: 'SDL.h' file not found
   32 | #include "SDL.h"
      |          ^~~~~~~
1 error generated.
make: *** [Makefile:16: obj/cleanup.o] Error 1

This is shell i’m using:

{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
    name = "SDL2";
    nativeBuildInputs = [
        SDL2
        SDL2_image
        pkg-config
    ];
    shellHook = ''
        LD_LIBRARY_PATH=${pkgs.SDL2}/lib
        LD_LIBRARY_PATH=${pkgs.SDL2_image}/lib
        echo $LD_LIBRARY_PATH
        # PKG_CONFIG_PATH=$PKG_CONFIG_PATH${pkgs.SDL2}/lib
        # PKG_CONFIG_PATH=$PKG_CONFIG_PATH${pkgs.SDL2_image}/lib
        # echo $PKG_CONFIG_PATH
        pkg-config --cflags --libs sdl2 SDL2_image
    '';
}

Thank you all :slight_smile:

my Makefile

CC = clang
FLAGS = -std=c11 -Wall -Wextra -Werror -Wpedantic
SDL_FLAGS = $(pkg-config --cflags --libs sdl2 SDL2_image)
NAME = endgame

SRC = $(wildcard ./src/*.c)
OBJ = $(patsubst ./src/%.c, ./obj/%.o, $(SRC))

all: $(NAME)

$(NAME): $(OBJ)
	$(CC) -o $(NAME) $(OBJ) $(FLAGS) $(SDL_FLAGS) -Iinc -Linc

./obj/%.o: ./src/%.c
	@mkdir -p ./obj
	$(CC) -c $< -o $@ $(FLAGS) $(SDL_FLAGS) -Iinc 

clean:
	@rm -rf ./obj/ $(NAME)

uninstall: clean

reinstall: uninstall all

Note how $(SDL_FLAGS) expanded to an empty list here.

GNU Make syntax for running external shell commands to store the result output in a variable should be (untested) SDL_FLAGS = $(shell pkg-config --cflags --libs sdl2 SDL2_image) instead of SDL_FLAGS = $(pkg-config --cflags --libs sdl2 SDL2_image).

1 Like

Thank you for your suggestion, this is reasonable indeed, but even with shell keyword the issue persists.

SDL_FLAGS = $(shell pkg-config --cflags --libs sdl2 SDL2_image)

all: $(NAME)

$(NAME): $(OBJ)
	@ar rcs $(NAME).a $(OBJ);
	$(CC) $(FLAGS) -o $(NAME) $(SDL_FLAGS) -lSDL2 -lSDL2_image -I inc src/$(NAME).c $(NAME).a

Can you create a minimal complete repository that fails to build? It might be easier to craft a patch against it to illustrate the fix.

1 Like

Yep, i can do it:
shell:

{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
    name = "SDL2";
    nativeBuildInputs = [
        SDL2
        SDL2_image
        pkg-config
    ];
    shellHook = ''
        LD_LIBRARY_PATH=${pkgs.SDL2}/lib
        LD_LIBRARY_PATH=${pkgs.SDL2_image}/lib
        echo $LD_LIBRARY_PATH
        # PKG_CONFIG_PATH=$PKG_CONFIG_PATH${pkgs.SDL2}/lib
        # PKG_CONFIG_PATH=$PKG_CONFIG_PATH${pkgs.SDL2_image}/lib
        # echo $PKG_CONFIG_PATH
        pkg-config --cflags --libs sdl2 SDL2_image
    '';
}

main:

#include <SDL2/SDL_surface.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

int main(void) {
    SDL_Surface *surface = IMG_Load("bee.png");
    SDL_FreeSurface(surface);
}

Compile with
clang ./*.c -lSDL2 -lSDL2_image

Also, this article is relative.

To be able to compile your project (on linux) I had to move the main.c to src/ directory and use make to build it. It built just fine:

$ make
gcc -c src/main.c -o obj/main.o  -D_REENTRANT -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include/SDL2 -I/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/include/SDL2 -L/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -L/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/lib -lSDL2_image -Wl,-rpath,/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -Wl,--enable-new-dtags -lSDL2 -Iinc
gcc -o endgame  ./obj/main.o  -D_REENTRANT -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include/SDL2 -I/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/include/SDL2 -L/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -L/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/lib -lSDL2_image -Wl,-rpath,/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -Wl,--enable-new-dtags -lSDL2 -Iinc -Linc

$ ls
endgame  Makefile  obj  shell.nix  src

If you run clang command directly you need to add a $(pkg-config --cflags --libs sdl2 SDL2_image) parameter to it as well.

1 Like

Can you share your Makefile, please?
And also, did you use shell for testing?

Full archive (828 bytes): https://slyfox.uni.cx/d/60116/a.tar.xz

Running:

$ wget 'https://slyfox.uni.cx/d/60116/a.tar.xz'
$ tar xf a.tar.xz

$ nix-shell --pure
/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/lib
-D_REENTRANT -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include/SDL2 -I/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/include/SDL2 -L/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -L/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/lib -lSDL2_image -Wl,-rpath,/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -Wl,--enable-new-dtags -lSDL2

$$ make
gcc -c src/main.c -o obj/main.o  -D_REENTRANT -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include/SDL2 -I/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/include/SDL2 -L/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -L/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/lib -lSDL2_image -Wl,-rpath,/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -Wl,--enable-new-dtags -lSDL2 -Iinc
gcc -o endgame  ./obj/main.o  -D_REENTRANT -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include -I/nix/store/25i5c4qivpfiqrxy239lsmdqm0pirwrl-SDL2-2.30.11-dev/include/SDL2 -I/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/include/SDL2 -L/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -L/nix/store/f4i4k26rl4qyxh18r9vajp0ab09vzvbq-SDL2_image-2.8.4/lib -lSDL2_image -Wl,-rpath,/nix/store/y0vi3axnpydl85krg5z3202fc9nww60z-SDL2-2.30.11/lib -Wl,--enable-new-dtags -lSDL2 -Iinc -Linc

$ ls
Makefile  a.tar.xz  endgame  obj  shell.nix  src
1 Like

Thank you.
It compiles on my end too)
But i still this issue with compiler in my project, maybe it’s on my side…

1 Like

Hi, the problem has been solved.
There were issues with my Makefile.
The shell:

{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
    name = "SDL2";
    nativeBuildInputs = [
        SDL2
        SDL2_image
        pkg-config
    ];
    shellHook = ''
        export LD_LIBRARY_PATH=${pkgs.SDL2}/lib:${pkgs.SDL2_image}/lib:$LD_LIBRARY_PATH
        export PKG_CONFIG_PATH=${pkgs.SDL2}/lib/pkgconfig:${pkgs.SDL2_image}/lib/pkgconfig:$PKG_CONFIG_PATH
        pkg-config --cflags --libs sdl2 SDL2_image
        '';
}

Fixed Makefile:

CC = clang
FLAGS = -std=c11 -Wall -Wextra -Werror -Wpedantic
CFLAGS := $(CFLAGS) `pkg-config --cflags sdl2 SDL2_image`
LDFLAGS := $(LDFLAGS) `pkg-config --libs sdl2 SDL2_image`
NAME = endgame
SRC = $(wildcard ./src/*.c)
OBJ = $(patsubst ./src/%.c, ./obj/%.o, $(SRC))

all: $(NAME)

$(NAME): $(OBJ)
	@ar rcs $(NAME).a $(OBJ);
	@$(CC) -o $(NAME) $(OBJ) $(FLAGS) $(LDFLAGS) -Iinc -Linc -lm $(NAME).a

./obj/%.o: ./src/%.c
	@mkdir -p ./obj
	@$(CC) -c $< -o $@ $(FLAGS) $(CFLAGS) -Iinc

clean:
	@rm -rf ./obj/*

uninstall: clean
	@rm -f $(NAME)
	@rm -f $(NAME).a
	@rm -rf ./obj/

reinstall: uninstall all

Big thanks @trofi :slight_smile:

1 Like