[英]Application runs fine when built from Xcode, won't run directly from the .app
Working on a c++ mac application in Xcode. 在Xcode中处理c ++ mac应用程序。 When I hit run everything is fine (debug and release mode). 当我点击运行时,一切都很好(调试和发布模式)。 But when I go into finder into the Products/Release folder and double click on the .app directly, the app immediately closes with an error (application quit unexpectedly. click report to see more detailed information). 但是,当我进入finder进入Products / Release文件夹并直接双击.app时,该应用程序立即关闭并出现错误(应用程序意外退出。单击报告以查看更多详细信息)。 When I click report, the line of code it errors on seems to indicate that it isn't finding my resource files, but it finds them fine when I build/run the same exact app from xcode. 当我单击报告时,出现错误的代码行似乎表明它找不到我的资源文件,但是当我从 xcode构建/运行相同的应用程序时,它发现它们很好。 heres an example of how i use the resource files in the code: 这是我如何在代码中使用资源文件的示例:
std::ifstream file;
file.open("AppName.app/Contents/Resources/Saves/1.svd");
//do stuff
file.close();
anyone have any ideas why double clicking the .app that xcode just created would have different results than running it from xcode? 有谁知道为什么双击刚创建的Xcode的.app与从xcode运行它会有不同的结果?
also not sure if this is part of the issue, but it seems strange that I have to reference the resource folder from outside the .app file (AppName.app/Contents/Resources/Saves/1.svd). 也不确定这是否是问题的一部分,但是我不得不从.app文件(AppName.app/Contents/Resources/Saves/1.svd)外部引用资源文件夹,这似乎很奇怪。 I would expect that path to be relative to the executable in .app/Contents/MacOS like this (../Resources/Saves/1.svd) but that didnt work either. 我希望该路径相对于.app / Contents / MacOS中的可执行文件是这样的(../Resources/Saves/1.svd),但也没有用。
I had similar problem and spending some time researching different options I ended up using Apple's objects. 我遇到了类似的问题,花了一些时间研究不同的选择,最终我还是使用了苹果公司的产品。 I needed to get resources folder: 我需要获取资源文件夹:
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
Then actual snippet is: 那么实际的代码片段是:
#ifdef __APPLE__
char path[FILENAME_MAX];
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)){
//
//log("CFURLGetFileSystemRepresentation returned false. Path:%s", path);
exit(0);
}
CFRelease(resourcesURL);
chdir(path);
//log("Current path:%s", path);
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.