C++ 如何在 Unix 中获取正在执行的 .so 模块的文件名(和路径) [英] C++ How to get a filename (and path) of the executing .so module in Unix

查看:36
本文介绍了C++ 如何在 Unix 中获取正在执行的 .so 模块的文件名(和路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 如何在 Unix 中获取正在执行的 .so 模块的文件名(和路径)?

C++ How to get a filename (and path) of the executing .so module in Unix?

类似于 Windows 上的 GetModuleFileName.

Something similar to GetModuleFileName on Windows.

推荐答案

虽然不是 POSIX 标准接口,dladdr() 函数可用于许多系统,包括 Linux, Solaris, Darwin/Mac OS XFreeBSD, HP-UXIRIX.这个函数需要一个地址,例如它可以是一个指向模块内静态函数的指针(如果转换为void *),并用信息填充Dl_info结构包括包含该地址的共享对象的路径名(在 dli_fname 成员中).

Although it is not a POSIX standard interface, the dladdr() function is available on many systems including Linux, Solaris, Darwin/Mac OS X, FreeBSD, HP-UX, and IRIX. This function takes an address, which could be a pointer to a static function within the module for example (if cast to void *), and fills in a Dl_info structure with information including the path name of the shared object containing that address (in the dli_fname member).

这篇关于C++ 如何在 Unix 中获取正在执行的 .so 模块的文件名(和路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆