简体   繁体   English

在Mac .app捆绑包中获取相对路径

[英]Getting relative paths within a Mac .app bundle

I've developed a game in C++, OpenGL and OpenAL and I am trying to build it into an app bundle rather than a command-line executable. 我已经用C ++,OpenGL和OpenAL开发了一个游戏,并且试图将其构建到应用程序包而不是命令行可执行文件中。 I copied and pasted the code from Relative Paths Not Working in Xcode C++ at the top of my main.cpp file (also tried it in the App_Prefix.pch) and I get a number of syntax errors. 我将main.cpp文件顶部的“ 相对路径无法在Xcode C ++工作”中的代码复制并粘贴到main.cpp文件的顶部(也在App_Prefix.pch中进行了尝试),并且遇到了许多语法错误。 I've annotated the code with the errors: 我用以下错误注释了代码:

#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

His code looks fine to me, and a lot of people have thanked him for it in his post. 他的代码对我来说看起来不错,许多人在他的帖子中对此表示感谢。 Is there anything I am doing wrong? 有什么我做错了吗? Should I create a file specifically for this code and import it before anything else? 我是否应该为此代码专门创建一个文件并将其导入?

Your code should be inside a function, for example main() . 您的代码应位于函数内部,例如main() Currently your code is pasted at the file level. 当前,您的代码粘贴在文件级别。 You can't have if statement or function calls there. 您不能在那里有if语句或函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM