【发布时间】:2018-05-07 12:39:54
【问题描述】:
背景:
我正在编写一个“Hello Rectangle”OpenGL 应用程序。我希望它是一个独立的.app,而不仅仅是一个“UNIX 可执行文件”。代码在 Terminal.app 中运行良好。
我没有使用 Xcode;我非常磁盘空间很紧,而且我没有做 iOS 开发。
相反,我使用的是 brewed 工具链,包括 gcc-8、glfw 和 glew。
我根据SO question 构建了我的发布目录,如下所示:
Contents
├── Frameworks
│ ├── libGLEW.2.1.dylib
│ ├── libgcc_s.1.dylib
│ ├── libglfw.3.dylib
│ └── libstdc++.6.dylib
├── MacOS
│ ├── hello-rect <- the actual binary
│ └── launcher.sh <- entry wrapper for working dir as the above link suggests
├── Resources
│ ├── rect.frag
│ └── rect.vert
└── Info.plist
我还在二进制文件中修改了对 dylibs 的引用。但它不会运行。
经过几个小时的谷歌搜索,SO answer 和SO question 以及其他几个人说服我应该使用CoreFoundation 来读取Resources 中的这两个着色器文件(文本文件)。
问题:
我尝试了以下代码(从上面的链接粘贴并稍作修改),但它出现了段错误:
const char* textFileReadCF(const char *&filename){
// Get a reference to the main bundle
CFBundleRef mainBundle = CFBundleGetMainBundle();
// Get a reference to the file's URL
CFStringRef filenameHandle = CFStringRef(filename);
// ------ segfault 11 here ------
CFURLRef fileURL = CFBundleCopyResourceURL(mainBundle, filenameHandle, NULL, NULL);
// Convert the URL reference into a string reference
CFStringRef filePath = CFURLCopyFileSystemPath(fileURL, kCFURLPOSIXPathStyle);
// Get the system encoding method
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
// Convert the string reference into a C string
const char *path = CFStringGetCStringPtr(filePath, encodingMethod);
// the real read function, read in file and returns all as a big char* arr
return textFileRead(path);
}
// this runs fine before packing and I didn't change,
// just for the sake of completeness
const char *textFileRead(const char *filename){
std::ifstream shaderFile(filename);
std::ostringstream shaderBuffer;
shaderBuffer << shaderFile.rdbuf();
std::string shaderBufferStr = shaderBuffer.str();
// Warning: safe only until shaderBufferStr is destroyed or modified
char * ret = new char[shaderBufferStr.size()];
std::strcpy(ret, shaderBufferStr.c_str());
return ret;
}
textFileReadCF 将被"rect.vert" 和"rect.vert" 调用两次(不是字面意思,而是const char* 持有它们)。
我不知道为什么会出现段错误。我确定有一个mainBundle 到assert(省略),并且我知道filenameHandle 是有效的,否则我不知道去哪里找。
【问题讨论】:
标签: macos opengl core-foundation