代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static void empty_dladdr()
{
//
}
char* getLocationPath(const char* pPath)
{
Dl_info dl_info;
dladdr((void*)empty_dladdr, &dl_info);
strcpy(pPath, dl_info.dli_fname);
return pPath;
}
有人问,使用NULL不行?确实不行。必须给个函数忽悠一下。猜测:
dladdr会运行empty_dladdr,趁机获取当前路径。
注意:如果是执行文件且使用相对路径,得得到的就是相对路径。需要用getcwd()获得绝对路径。