I am attempting to load some images into a very basic C++/SDL based program. The images are loaded using SDL_LoadBMP. I have the image loading and everything seems to work perfectly. My problem is that the image is loaded relative to where I call the program from.
So I have a directory structure that goes something like this.
project/
src/
source.cpp
assets/
image.bmp
bin/
program
When I execute the program from the project directory (./bin/program) it has to use a path of assets/image.bmp to load the image. Is there any way I can use a relative path of ../assets/image.bmp so the program can be executed from anywhere?
I think I need something that provides the absolute path to the program that I can then append my paths to.
On Linux,
/proc/self/exeis a symbolic link to the executable of the current process. So you can usereadlinkto find its path. From there you can use usual path manipulation to find your resources. Perhaps you should cater for the case where the program executable is again a symlink to the location where your binary lies, i.e. do some more realink as long as the current path is a symlink.There once was a question about finding current executable’s path without
/proc/self/exewhich also asked about a portable interface, but no single solution that caters for all needs was given in the accepted answer.