Get executable’s path (with std::filesystem) [duplicate]

Before i get marked as duplicate, all similar questions i could find have answers prior to the introduction of std::filesystem, and either use platform-specific code or Boost::filesystem. I’m looking for a portable answer that uses std::filesystem.


Is it possible to get the path where a c++ executable is located (not the working directory) using std::filesystem? If it is, how?

  • 3

    I can’t spot anything here. So I am afraid the answer is, it’s not possible.

    – 

  • How about the second parameter (argv) of the main function? I’m not sure how portable that is.

    – 

  • @Kerndog73 That won’t include the full path unless the program was called using a full path.

    – 

  • 2

    @Kerndog73 Definitely not with linux systems. It’s just what was typed in at the shell, or used in a UI launcher.

    – 




  • 3

    You can get the absolute path using std::filesystem and the current directory and argv[0] but it is stil not “portable” as nothing says what goes in argv[0] in the C++ Standard.

    – 




No, there’s nothing provided in the standard filesystem facilities to get the path of your executable.

Even using using the 1st argv argument isn’t guaranteed to contain the full path of the executable.
The systems I know will just pass in the string that was used to launch the program.
Considering that this could be resolved using the PATH environment variable, there’s no guarantee, you see a full path there.

There are some OS specific methods to do so though:

  • Get path of executable

Leave a Comment