# Why can't I load an image with a relative path in SDL2 on Linux?

When loading a texture in SDL2 the general procedure is this:

SDL_Texture* texture = IMG_LoadTexture(renderer, "resources/images/image.png);


However this doesn't work and I have to supply the WHOLE path to the specific image. The code that I have to use is:

SDL_Texture* texture = IMG_LoadTexture(renderer, "/home/user/programs/game/resources/images/image.png);


This issue doesn't occur in a Windows Mingw32 environment.

Any suggestion on how to fix this issue?

It's possible that you're running the executable in a different directory than the one that contains your resources folder.

If you need to change the directory from, say, a makefile, just cd to the desired base directory and run the executable from there.

• Instead of makefile, the recommended way is to use SDL_GetBasePath() to access resources. Feb 10 '15 at 0:05

You can get this information with the following code:

char buffer[256];
char *getcwd( buffer, 256 );


One hint: when the working directory differs from the place where the binary is stored: do you start the app from an IDE like QtCreator. If yes: please change the working directory for the run in your IDE.

• Your hint helped also but the other answer was better Aug 12 '14 at 14:29