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?
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
I can’t spot anything here. So I am afraid the answer is, it’s not possible.
How about the second parameter (
argv
) of themain
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.
@Kerndog73 Definitely not with linux systems. It’s just what was typed in at the shell, or used in a UI launcher.
You can get the absolute path using
std::filesystem
and the current directory andargv[0]
but it is stil not “portable” as nothing says what goes inargv[0]
in theC++
Standard.Show 4 more comments