Linux C(++)獲取可執行程序完整路徑


代碼


#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;

}

輸出結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM