-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutableUtils.cpp
More file actions
35 lines (27 loc) · 938 Bytes
/
Copy pathexecutableUtils.cpp
File metadata and controls
35 lines (27 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// executableUtils.cpp
#include "executableUtils.hpp"
#include <filesystem>
#include <string>
#include <stdexcept>
namespace fs = std::filesystem;
[[nodiscard]] std::filesystem::path get_executable_path()
{
// On Linux → /proc/self/exe is a symlink to the actual binary
static const fs::path proc_self_exe{"/proc/self/exe"};
std::error_code ec;
auto canonical_path = fs::canonical(proc_self_exe, ec);
if (ec)
{
throw std::runtime_error(
"Failed to canonicalize /proc/self/exe: " + ec.message());
}
return canonical_path;
}
[[nodiscard]] std::string get_executable_filename()
{
// Most common use-case is just the filename → we cache the full path
static const auto cached_path = get_executable_path();
// .filename() → returns path (may be empty)
// .string() → std::string (empty if no filename component)
return cached_path.filename().string();
}