C++ 正在检索当前/自己的捆绑包的路径
在编写跨平台模块时,我需要一种检索模块目录路径的方法(这样我就可以访问modules文件夹中的文件,如configs和resources)。我的模块当前编译为一个包,由任意进程加载。在windows中,获取模块路径非常容易,如下所示:C++ 正在检索当前/自己的捆绑包的路径,c++,objective-c,macos,core-foundation,nsbundle,C++,Objective C,Macos,Core Foundation,Nsbundle,在编写跨平台模块时,我需要一种检索模块目录路径的方法(这样我就可以访问modules文件夹中的文件,如configs和resources)。我的模块当前编译为一个包,由任意进程加载。在windows中,获取模块路径非常容易,如下所示: GetModuleFileName(GetModuleHandle(0), buf, size); // copies the path of the current DLL into buf 我还没有在OSX上找到类似的方法。我尝试了\u NSGetExecu
GetModuleFileName(GetModuleHandle(0), buf, size); // copies the path of the current DLL into buf
我还没有在OSX上找到类似的方法。我尝试了\u NSGetExecutablePath()
,但它只检索主程序的路径。也试过了
但是,它还获取主可执行文件的路径。在OSx上执行此操作的常用方法是什么
问候
编辑:
grady player向我展示了方法:)我使用NSBundle编译了以下代码:
/*
this could be anything apparantly
*/
@interface dummyObject : NSObject
- (void) dummyMethod;
@end
@implementation dummyObject
- (void) dummyMethod {
}
@end
int GetBundlePath(char * buf, int bufSize)
{
NSString * path = [[NSBundle bundleForClass:[dummyObject class]]bundlePath];
const char * intString = [path UTF8String];
int length = strlen(intString);
int smallestLength = length > bufSize ? bufSize : length;
memcpy(buf, intString, smallestLength);
[path release];
return smallestLength;
}
将为动态库工作
或者如果你正在寻找你的主要
[[NSBundle mainBundle] bundlePath];
另外,我还。。。见
那么您的资源应该是相对于您的主捆绑包的。
你可以用
[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponents:@[@"Contents",@"Resources"]];
非常感谢你。我对所有Objto-C相关(完全使用C++)完全陌生,请在第一个例子中扩展一点(我很确定这就是我想要的)。具体来说,thisClass指的是什么-由返回的包实例化的类?我看到一些人这样做,这有效吗?NSBundle*bundle=[NSBundle bundleForClass:[self class]];之后,我会对返回的bundle调用bundlePath来检索到它的路径?@Shaggi class既是一个类又是一个实例方法,所以任何本地类或实例。。。例如,
[MyDog class]
或[aDog class]
应该返回相同的内容…我让它工作了-非常感谢-用工作示例更新了主题。
[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponents:@[@"Contents",@"Resources"]];