Finding current executable's path without /proc/self/exe

前端 未结 13 828
有刺的猬
有刺的猬 2020-11-22 01:06

It seems to me that Linux has it easy with /proc/self/exe. But I\'d like to know if there is a convenient way to find the current application\'s directory in C/C++ with cros

13条回答
  •  你的背包
    2020-11-22 01:36

    An alternative on Linux to using either /proc/self/exe or argv[0] is using the information passed by the ELF interpreter, made available by glibc as such:

    #include 
    #include 
    
    int main(int argc, char **argv)
    {
        printf("%s\n", (char *)getauxval(AT_EXECFN));
        return(0);
    }
    

    Note that getauxval is a glibc extension, and to be robust you should check so that it doesn't return NULL (indicating that the ELF interpreter hasn't provided the AT_EXECFN parameter), but I don't think this is ever actually a problem on Linux.

提交回复
热议问题