Osx App Bundle Resource Path
Osx App Bundle Resource Path
简介
用于在OSX的.app程序中得到Resources文件夹的绝对路径,来正确载入资源文件
只需链接CoreFoundation即可,代码来自SFML论坛[0]
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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.
This post is licensed under CC BY 4.0 by the author.