0

Working on a c++ mac application in Xcode. 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). 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. 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?

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). 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.

Dylan
  • 45
  • 1
  • 8
  • The current directory might be different when using Xcode and when explicitly changing it in Finder. – Bo Persson Jun 24 '16 at 06:14
  • @BoPersson I'm not sure I understand, could you explain what you mean by current directory? To clarify, I'm not moving the .app file to a different folder and then double clicking it, I'm going into the folder that xcode created when I hit run and clicking on the .app that it just created. or are you saying "current directory" is a setting i need to change somewhere? – Dylan Jun 24 '16 at 06:22
  • ended up getting it to run by just putting in an absolute path on the files (i went ahead and assumed the .app file was in /Applications) as suggested here: http://stackoverflow.com/questions/26161144/differences-between-running-osx-app-from-finder-versus-xcode however, id still like to know how to do it more stable way that doesnt assume the .app is in a specific place on the computer. if anybody knows that, would love to know how. thanks – Dylan Jun 24 '16 at 07:17
  • I meant exactly that. A relative path is relative to some "current directory", which doesn't have to be the place where the program resides. When you click on a folder i Finder, you change the current directory. Xcode might change it to something else, like the project's directory. – Bo Persson Jun 24 '16 at 07:45

1 Answers1

0

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
Eugene Mankovski
  • 1,180
  • 10
  • 16
  • thanks! im getting linker errors though when using this – Dylan Jun 27 '16 at 18:45
  • Undefined symbols for architecture x86_64: "_CFBundleCopyResourcesDirectoryURL", referenced from: Files::changeDirectoryToResourcesFolder() in Files.o "_CFBundleGetMainBundle", referenced from: Files::changeDirectoryToResourcesFolder() in Files.o "_CFRelease", referenced from: Files::changeDirectoryToResourcesFolder() in Files.o "_CFURLGetFileSystemRepresentation", referenced from: Files::changeDirectoryToResourcesFolder() in Files.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 – Dylan Jun 27 '16 at 18:45
  • is there a framework or somethign somewhere i need to include to use these? – Dylan Jun 27 '16 at 18:46
  • duh, nevermind. CoreFoundation.framework. thanks for the help! – Dylan Jun 27 '16 at 19:23
  • @Dylan Yes sorry I forgot to mention that you need to link CoreFoundation library. That was so long ago... – Eugene Mankovski Jun 27 '16 at 22:18