How to get path to current exe file on Linux?

c++, exe, path

Solution

dirname is the function you're looking for.

#include <libgen.h>         // dirname
#include <unistd.h>         // readlink
#include <linux/limits.h>   // PATH_MAX

char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
const char *path;
if (count != -1) {
    path = dirname(result);
}

Problem

The code below gives current path to exe file on Linux: ``` #include <iostream> std::string getExePath() { char result[ PATH_MAX ]; ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX ); return std::string( result, (count > 0) ? count : 0 ); } int main() { std::cout << getExePath() << std::endl; return 0; } ``` The problem is that when I run it gives me current path to exe and name of the exe, e.g.: ``` /home/.../Test/main.exe ``` I would like to get only ``` /home/.../Test/ ``` I know that I can parse it, but is there any nicer way to do that?

Original source

Related problems