代码


#include <sys/statfs.h>
#include <string>
#include <iostream>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/// get executable path
std::string get_cur_executable_path_()
{
    char *p                 = NULL;

    const int len           = 256;
    /// to keep the absolute path of executable's path
    char arr_tmp[len]       = {0};
    
    int n                   = readlink("/proc/self/exe", arr_tmp, len);
    if (NULL                != (p = strrchr(arr_tmp,'/')))
        *p = '\0';
    else
    {
        return std::string("");
    }

    return std::string(arr_tmp);
}
 
int main(int argc, char* argv[], char* env[])
{
    std::string exec_str    = get_cur_executable_path_();
    std::cout << "str=" << exec_str << "\n\n";
    return 0;

}

输出结果

Linux C(++)获取可执行程序完整路径

相关文章:

  • 2021-06-20
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2021-12-07
  • 2021-07-24
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案