【问题标题】:Getting relative paths within a Mac .app bundle在 Mac .app 包中获取相对路径
【发布时间】:2011-11-19 15:38:50
【问题描述】:

我已经用 C++、OpenGL 和 OpenAL 开发了一款游戏,我正在尝试将它构建到一个应用程序包中,而不是一个命令行可执行文件中。我从 Relative Paths Not Working in Xcode C++ 复制并粘贴了 main.cpp 文件顶部的代码(也在 App_Prefix.pch 中尝试过),我得到了一些语法错误。我已经用错误注释了代码:

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif

// ----------------------------------------------------------------------------
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__    
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)) // Error: expected unqualified-id before 'if'
    {
        // error!
    }
    CFRelease(resourcesURL); // error: expected constructor, destructor or type conversion before '(' token

    chdir(path); // error: expected constructor, destructor or type conversion before '(' token
    std::cout << "Current Path: " << path << std::endl; // error: expected constructor, destructor or type conversion before '<<' token
#endif

他的代码在我看来很好,很多人在他的帖子中感谢他。有什么我做错了吗?我是否应该专门为此代码创建一个文件并先导入它?

【问题讨论】:

    标签: c++ xcode macos


    【解决方案1】:

    您的代码应该在函数内部,例如main()。目前,您的代码粘贴在文件级别。那里不能有if 语句或函数调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2011-02-07
      • 2012-10-25
      相关资源
      最近更新 更多