【问题标题】:C++: Unable to read resource files in macos bundleC++:无法读取 macOS 包中的资源文件
【发布时间】: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


    【解决方案1】:

    原来我确实不需要需要CoreFoundation。我只需要GLFW。

    在早期版本中,我的发布目录如下所示:

    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
    │   ├── rect.frag
    │   └── rect.vert
    └── Info.plist
    

    因为未捆绑的版本显然除了cwd 之外别无他处。 我什至不知道有Resources。当时我的代码也没有运行;我的谷歌跟踪让我相信CoreFoundation 是解决方案。直到我检查了有关 CoreFoundation 的文档,我才知道应该有 Resources。我偶然发现了这个SO question,当时所有的点都连接在一起。

    显然我的GLFW 版本的行为也类似于链接状态:(直接来自源代码)

    // 切换到我们的应用程序包的资源目录(如果存在)

    因此,使捆绑包开箱即用所需要做的就是,只需像讨论的那样构建发布目录,然后使用"rect.vert" 和"rect.vert" 直接调用textFileRead,我们就都准备好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-05
      • 2014-06-07
      • 1970-01-01
      • 2018-11-12
      • 2023-03-24
      • 2011-10-12
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多