SDL_image.h fatal error: No such file or directory

Solution for SDL_image.h fatal error: No such file or directory
is Given Below:

I am using Debian and i downloaded SDL_image.h succesfully. (sudo apt-get install libsdl2-image-dev)

I wrote a simple code to tell if it sees PNG images, but I’m getting an error.

Code:

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


int main(){
    if (SDL_Init(SDL_INIT_VIDEO) < 0) printf("ERROR SDL_Init() - VIDEO");
    if (IMG_Init(IMG_INIT_PNG) < 0) prinft("ERROR IMG_Init() - PNG");
    
    char fileName[50] = "im.PNG";
    SDL_Texture *image = IMG_Load(fileName);
    if (image == NULL) printf("ERROR image == NULL");

    SDL_FreeSurface(image);
    return 0;
}

I compiled it on the command line as follows

gcc SDL_learnT.c -w -lSDL2 -o SDL_learnT

And i am getting Error = “fatal error: SDL_image.h No such file or directory”

I tried to do the following but the result did not change
#include <SDL2_image.h> or #include <SDL2/SDL_image.h>

Edit: It seems from your latest edit that you’ve [already] solved your problem, so the following may be moot.


Install the development package for SDL2_image [which it appears you’ve already done–sigh].

On fedora, this is:

sudo dnf install SDL2_image-devel

On ubuntu:

sudo apt install libsdl2-image-dev

Use pkg-config in the gcc lines (e.g.):

gcc -o program program.c `pkg-config --cflags --libs` SDL2_image

or sdl2-config:

gcc -o program program.c `sdl2-config --cflags --libs` -lSDL2_image

In any case, the correct include is:

#include <SDL2/SDL_image.h>

You should be able to do:

find -xdev /usr -name SDL_image.h
find -xdev /usr/local -name SDL_image.h

Or, some ls commands.

Then, compare against the pkg-config output.

A last resort … I’ve had trouble in the past with SDL2 and ubuntu (bionic). Ultimately, I uninstalled the standard packages and rebuilt/reinstalled from the source packages.

OT:

IMG_Load returns a surface, not a texture:

SDL_Texture *image = IMG_Load(fileName);

should be

SDL_Surface *image = IMG_Load(fileName);

And here:

if (SDL_Init(SDL_INIT_VIDEO) < 0) printf("ERROR SDL_Init() - VIDEO");

It is not enough to inform about the error, you should exit (or at least skip all SDL functions), a better approach:

if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
    SDL_Log("SDL_Init: %s", SDL_GetError());
    exit(EXIT_FAILURE);
}

SOLUTION

It should be <SDL2/SDL_image.h> not <SDL_image.h>

Compiling with gcc should be as follows (Command Line)

$gcc FILENAME.c -o OUTNAME -w -lSDL2 -lSDL2_image