Application bundle path
-
The Qt docs say:
Accessing the Bundle PathmacOS applications are structured as a directory (ending with .app). This directory contains sub-directories and files. It may be useful to place items, such as plugins and online documentation, inside this bundle. The following code returns the path of the application bundle:
#ifdef Q_OS_MAC QString bundlePath = QString::fromNSString(NSBundle.mainBundle.bundlePath); qDebug() << "Bundle path =" << bundlePath; #endifbut as that stands I get:
/Users/amonra/.vs/DSS/DeepSkyStacker/DeepSkyStacker.cpp:602:44: error: use of undeclared identifier 'NSBundle' 602 | QString bundlePath{ QString::fromNSString(NSBundle.mainBundle.bundlePath) };Clearly there's a header file I need to make that work - what do I need to include?
Thanks
David -
Hi,
NSBundle comes from the Foundation framework.
-
Well, Cocoa APIs are in Objective-C so can be accessed only via that language, or something that bridges to it (like Swift).
The example in the documentation seems wrong. It looks like Swift code, which never mixes with C++ in the same file.
Your best best is to make the file compile as Objective-C++, by changing its extension to
.mm. In such files you can freely mix C++ and Objective-C code, and the line will be something like:NSString* path = [[NSBundle mainBundle] bundlePath]; QString bundlePath = QString::fromNSString(path);After
#import-ing the Foundations library of course. The downside is that now that file compiles only on macOS with Xcode, so you probably want to have one/few files in your project only for specific Mac utility methods.There are also very low-level ways to send messages to Objective-C objects directly from C/C++ without compiling with the Objective-C compiler, but you probably don't want to go there.
-
OK I don't really wish to go there :(
I suppose I could write a .mm "helper function" but in the end I gave up on the recommendation in the docs and used
QString appPath{ QCoreApplication::applicationDirPath() };
to get the location of the macOS subfolder and navigated from there.Is there a better way?
-
OK I don't really wish to go there :(
I suppose I could write a .mm "helper function" but in the end I gave up on the recommendation in the docs and used
QString appPath{ QCoreApplication::applicationDirPath() };
to get the location of the macOS subfolder and navigated from there.Is there a better way?
@Perdrix QStandardPaths with
QStandardPaths::ApplicationsLocationmight be what you are looking for. -
Sorry, I've misunderstood your goal. So QCoreApplication::applicationDirPath is the simple way to do it.