且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-09 17:48:54

虽然不是 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).