Osx App Bundle Resource Path
Posted In 23 Jan 2016 Tagged With CPP Notes
简介
用于在OSX的.app程序中得到Resources文件夹的绝对路径,来正确载入资源文件
只需链接CoreFoundation即可,代码来自SFML论坛[0]
代码
#if defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
#endif
std::string ResourcePath()
{
#if defined(__APPLE__)
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[512];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8*)path, 512))
{
// error!
std::cout << "Path not found!" << std::endl;
}
CFRelease(resourcesURL);
return std::string(path);
#else
return std::string();
#endif
}
附录
[0] SFML forum post Load resources in app bundle.
[1] Apple reference Accessing a Bundle’s Contents.