PDA

View Full Version : Where's the app bundle looking for my media folder? Ok on Qt Creator not in Terminal.



toglia3d
3rd October 2011, 23:02
I'm having a little trouble making my (mac os x) "app bundle" work from the terminal or just double clicking it.

This App actually compiles, links and runs perfectly within the Qt Creator IDE. But, if I try to open it from the terminal I get a "media/file.x file not found" error. The App bundle nor the /Contents/MacOS/executable is finding the "media" folder that is supposed to be beside the executable.

In my app I do something like:

openFile("media/file.x");

In Windows and Linux, this file WILL be found if the "media" folder is exactly in the same hierarchical position of the executable (beside it). On the Mac I have discovered it works differently cause Qt Creator builds an "App Bundle" and the actual executable is inside the /Contents/MacOS folder, so I copied the "media" manually there. This worked without any hassle when "playing" my app from the Qt Creator but as mentioned before it doesn't work when using the bundle itself.

So does anyone know where or how can I homogenize the look for this "media" folder so it works on both Qt Creator and the App bundle itself?

Lately, I have been using the following command to "install" the folder on the bundle.

mac {
MediaFiles.files = media
MediaFiles.path = Contents/MacOS
QMAKE_BUNDLE_DATA += MediaFiles
}

Thanks for your help.

toglia3d
5th October 2011, 18:30
After looking for a few days I found a couple of posts discussing the relative path problem. I was just not searching with the right words... The answer is displayed in:

http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c

http://www.experimentgarden.com/2009/06/how-to-load-resource-from-your.html

Basically it is necessary to add this:



//On the include part
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif


// On the main of your app
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);

chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------

**IMPORTANT:
The chdir, changes the working path of the app, so it is not necessary to change you relative code after that...**