8

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?

Galik
  • 47,303
  • 4
  • 80
  • 117
Barnack
  • 921
  • 1
  • 8
  • 20
  • 3
    I can't spot anything [here](https://en.cppreference.com/w/cpp/filesystem). So I am afraid the answer is, it's not possible. – πάντα ῥεῖ May 04 '19 at 12:37
  • How about the second parameter (`argv`) of the `main` function? I’m not sure how portable that is. – Indiana Kernick May 04 '19 at 13:11
  • @Kerndog73 That won't include the full path unless the program was called using a full path. – πάντα ῥεῖ May 04 '19 at 13:20
  • [cppreference](https://en.cppreference.com/w/cpp/language/main_function) says that `argv[0]` is the “name used to invoke the program”. Assuming that this is an absolute path might not be portable but I really have no idea. All I can say is that on macOS it’s an absolute path. – Indiana Kernick May 04 '19 at 13:21
  • @πάνταῥεῖ It’s always an absolute path on macOS but probably not elsewhere – Indiana Kernick May 04 '19 at 13:23
  • 2
    @Kerndog73 Definitely not with linux systems. It's just what was typed in at the shell, or used in a UI launcher. – πάντα ῥεῖ May 04 '19 at 13:25
  • How about `.`? Or does it have to be an absolute path? – L. F. May 04 '19 at 13:28
  • 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. – Galik May 04 '19 at 13:30
  • I think that [`std::filesystem::absolute`](https://en.cppreference.com/w/cpp/filesystem/absolute) will do what you want it to do (construct a `std::filesystem::path` object using `argv[0]`). – Phil Brubaker May 04 '19 at 16:57

1 Answers1

7

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:

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • You could use [current_path](https://en.cppreference.com/w/cpp/filesystem/current_path) to resolve relative paths to an absolute path. But even then it is `OS` dependent. Though it should work on most systems I would think. – Galik May 04 '19 at 13:53
  • 5
    @Galik The current working directory has absolutely nothing to do with the executable's path. – πάντα ῥεῖ May 04 '19 at 13:55
  • Not on its own. You would still have to use it in conjunction with `PATH` in some instance. – Galik May 04 '19 at 14:04