Programmatically retrieving the absolute path of an OS X command-line app

后端 未结 7 496
予麋鹿
予麋鹿 2020-11-27 15:04

On Linux, an application can easily get its absolute path by querying /proc/self/exe. On FreeBSD, it\'s more involved, since you have to build up a sysctl call

7条回答
  •  眼角桃花
    2020-11-27 15:25

    There is no guaranteed way I think. If argv[0] is a symlink then you could use readlink(). If command is executed through the $PATH then one could try some of: search(getenv("PATH")), getenv("_"), dladdr()

提交回复
热议问题