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

前端 未结 13 1114
有刺的猬
有刺的猬 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:35

    Depending on the version of QNX Neutrino, there are different ways to find the full path and name of the executable file that was used to start the running process. I denote the process identifier as . Try the following:

    1. If the file /proc/self/exefile exists, then its contents are the requested information.
    2. If the file /proc//exefile exists, then its contents are the requested information.
    3. If the file /proc/self/as exists, then:
      1. open() the file.
      2. Allocate a buffer of, at least, sizeof(procfs_debuginfo) + _POSIX_PATH_MAX.
      3. Give that buffer as input to devctl(fd, DCMD_PROC_MAPDEBUG_BASE,....
      4. Cast the buffer to a procfs_debuginfo*.
      5. The requested information is at the path field of the procfs_debuginfo structure. Warning: For some reason, sometimes, QNX omits the first slash / of the file path. Prepend that / when needed.
      6. Clean up (close the file, free the buffer, etc.).
    4. Try the procedure in 3. with the file /proc//as.
    5. Try dladdr(dlsym(RTLD_DEFAULT, "main"), &dlinfo) where dlinfo is a Dl_info structure whose dli_fname might contain the requested information.

    I hope this helps.

提交回复
热议问题