c++ - How to get path to current exe file on Linux? -
this question has answer here:
- c/c++ - executable path 5 answers
the code below gives current path 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 when run gives me current path exe , name of exe, e.g.:
/home/.../test/main.exe
i
/home/.../test/
i know can parse it, there nicer way that?
dirname function you're looking for.
#include <libgen.h> ssize_t count = readlink("/proc/self/exe", result, path_max); const char *path; if (count != -1) { path = dirname(result); }
Comments
Post a Comment