2

When running an executable, relative paths are relative to the directory the executable is launched in, not where the executable is. This means that any resource I'm loading can only be found if I launch the app from the directory it is in. What's the best way to go about that in C/C++, in order to be able to run the executable from any directory?

Do I have to save the executable's absolute path upon first launch and append it at the beginning of every relative path?

Leizi
  • 33
  • 4
  • You can get the path of your executable: https://stackoverflow.com/questions/1528298/get-path-of-executable This allows easily create a file path relative to the executable using the functionality provided in the `std::filesystem` namespace... – fabian Jan 27 '22 at 20:23
  • thanks, I guess I'll have to get the absolute path at launch and append it at the beginning of every relative one – Leizi Jan 27 '22 at 20:30
  • Technically you could change the working directory for the process (https://en.cppreference.com/w/cpp/filesystem/current_path), but I wouldn't recommend this, since this isn't what you'd expect to happen in general... – fabian Jan 27 '22 at 20:31

0 Answers0