操作系统得到可执行路径
下面这个函数是从bullet里面摘出来的,其他的方式还有一个就是使用whereami,不过bullet比较简单,适用于苹果,linux和windows, whereami的函数比较全面,可以自动检测系统获取,下面是代码
int getExePath(char* path, int maxPathLenInBytes)
{
int numBytes = 0;
#if __APPLE__
uint32_t bufsize = uint32_t(maxPathLenInBytes);
if (_NSGetExecutablePath(path, &bufsize) != 0)
{
b3Warning("Cannot find executable path\n");
return false;
}
else
{
numBytes = strlen(path);
}
#else
#ifdef _WIN32
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
HMODULE hModule = GetModuleHandle(NULL);
numBytes = GetModuleFileNameA(hModule, path, maxPathLenInBytes);
#else
///http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c
numBytes = (int)readlink("/proc/self/exe", path, maxPathLenInBytes - 1);
if (numBytes > 0)
{
path[numBytes] = 0;
}
else
{
b3Warning("Cannot find executable path\n");
}
#endif //_WIN32
#endif //__APPLE__
return numBytes;
}