1
General / Re: Executable bundled into mac app package crashes
« on: March 01, 2017, 05:57:32 pm »It is intentionally not portable because it cannot be. (Well, the definition is not portable but the declaration is.)
Sorry to revive an old thread, but I just realized that there is a pure C++ solution for this, although it's hacky and still non-portable:
#ifdef APPLE
// -framework Cocoa
#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
std::string ResourcePath() {
id pool = reinterpret_cast<id>(objc_getClass("NSAutoreleasePool"));
std::string rpath;
if (!pool) {
return rpath;
}
pool = objc_msgSend(pool, sel_registerName("alloc"));
if (!pool) {
return rpath;
}
pool = objc_msgSend(pool, sel_registerName("init"));
id bundle =
objc_msgSend(reinterpret_cast<id>(objc_getClass("NSBundle")),
sel_registerName("mainBundle"));
if (bundle) {
id path = objc_msgSend(bundle, sel_registerName("resourcePath"));
rpath =
reinterpret_cast<const char *>(objc_msgSend(path, sel_registerName("UTF8String"))) +
std::string("/");
}
objc_msgSend(pool, sel_registerName("drain"));
return rpath;
}
#elseif WIN32
// ...
#endif
It simplifies things for me anyway though, because I don't have to link with the objective c runtime or conditionally add source files to the executable target in my CMake script depending on platform (just link the Cocoa framework).